25 Commits
Author SHA1 Message Date
YourWishesandClaude Sonnet 5 44594f7011 Add cutsceneAppend/cutsceneAppendNext for mutating a running cutscene
cutsceneAppend adds items to the end of a cutscene_t's item array;
cutsceneAppendNext inserts them at a given index instead (e.g.
CUTSCENE_SYSTEM.currentItem + 1, to run immediately after the
currently-executing item), shifting everything from that index
onward to make room. Both are capacity-bounded against the caller-
supplied itemsMax, matching cutsceneParseDoc's convention of never
assuming ownership of the backing buffer's size.

cutscene_t.items drops its const qualifier to allow this in-place
mutation; cutsceneitem_t backing arrays in the test fixtures that get
assigned into a mutable cutscene_t.items are no longer declared const
either, since a const source would otherwise silently violate the
field's new type.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-15 13:16:02 -05:00
YourWishesandClaude Sonnet 5 dbb5301ade Extract battleactiontype_t/battleaction_t into battleaction.h
Matches the existing battleability.h/battlefighter.h split - keeps
battle.h focused on the overall battle_t state machine rather than
also owning the action data shape.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-14 12:28:40 -05:00
YourWishesandClaude Sonnet 5 74a4290be4 Add optional cutscene field to move schema
Lets a move reference a cutscene to play when used, resolved by name
the same way a CUTSCENE item's "name" field is. Still just a schema
placeholder - no loader consumes moves.jsonc yet.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-14 12:10:06 -05:00
YourWishesandClaude Sonnet 5 3ce892f846 Move items.jsonc under items/, add starter battle/moves.jsonc
Groups item data under assets/items/ to match the cutscenes/ folder
convention, updating itemInit()'s asset path accordingly. Also adds
assets/battle/moves.jsonc as a starting point for move data - there's
no JSON loader consuming it yet (battleability_t is still just a bare
enum), so its field shape is a placeholder rather than a contract.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-14 12:00:46 -05:00
YourWishesandClaude Sonnet 5 68cebce04a Remove per-item cutscene authoring macros
Every CUTSCENE_XXX(...) shorthand (CUTSCENE_WAIT, CUTSCENE_TEXT,
CUTSCENE_ENTITY_WALK_TO, etc.) existed to build a cutsceneitem_t literal
in C; real cutscenes are now authored exclusively as .jsonc files, and
the only other users were test fixtures. Deletes all ~55 of these
macros (and the shared CUTSCENE_ITEM(...) helper they built on) along
with their doc comments, leaving each item's struct/enum/Start/Update/
Load declarations untouched.

test_battle.c, test_cutscenecontrol.c, test_cutscenemaparea.c,
test_cutscenesystem.c and test_entityinteract.c now build their
cutsceneitem_t fixtures as plain struct literals instead.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-14 11:20:58 -05:00
YourWishesandClaude Sonnet 5 1646e964b5 Remove the C-authoring shorthand for declaring cutscenes
CUTSCENE(...)/CUTSCENE_REFERENCE/CUTSCENE_CUTSCENE/CUTSCENE_INSERT
existed to build a cutscene_t and its item array directly in C; real
cutscenes are now authored exclusively as .jsonc files parsed via
cutsceneParseDoc, so these are dead weight in production. Deletes the
demo scene that still used them (testcutscene.h, wired to NPC #3) along
with its now-dead include in npc.c. test_cutscenesystem.c - the only
other user, building cutscenes in-memory for tests - now writes the
equivalent struct literals directly, matching the convention already
used by test_battle.c/test_entityinteract.c.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-14 09:52:42 -05:00
YourWishesandClaude Sonnet 5 2ee97ad4ce Sunset the dedicated cutscene asset loader and the item pool
Cutscenes now load through the generic ASSET_LOADER_TYPE_JSON loader
and parse straight into fixed static buffers via new cutsceneParseDoc/
cutsceneParseItem/cutsceneCountItems helpers (cutscene.c), removing the
bespoke ASSET_LOADER_TYPE_CUTSCENE pipeline entirely. Only one cutscene
(plus one INSERT target) is ever resident, so each resolve locks,
steals the parsed doc, unlocks, and force-reaps immediately - fixing a
stale-cache-reuse bug where revisiting a cutscene name after a
different one could return data from the shared buffer's previous
occupant.

ENTITY_WALK_TO and MAP_AREA_WAIT now store their waypoints/area ids in
fixed-size arrays inline on their own item struct instead of a shared
bump-allocated pool, so pool/poolOffset are gone from every item type's
Load callback. cutscene_t.dataSize (and the CUTSCENE(...) macro's SIZE
parameter) is removed along with it - it sized entries out of that same
now-deleted pool and had no other reader.

NPC #4's battletest cutscene and the main menu's cutscene now resolve
their target by name on interact instead of eagerly at init, so a
resolved-once pointer is never held past the point its backing buffer
can be reused for something else.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-13 15:18:57 -05:00
YourWishesandClaude Sonnet 5 109ddc9e1e Switch item definitions to a JSON-driven runtime registry
Replaces the build-time item.json/tools.item.py code generator with a
single item_t ITEM registry populated at runtime by parsing
assets/items.jsonc, so items/types can grow without a rebuild. Adds
itemGetDefById/itemGetTypeById/itemGetIdByName/itemTypeGetOrCreate,
updates inventory/backpack to size off the new *_DEF_COUNT_MAX caps
and look up types dynamically, and reorders engine init so rpgInit
(which loads items) runs before uiInit (which needs the item type
count to size the backpack's tabs).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-13 11:36:50 -05:00
YourWishesandClaude Sonnet 5 804f8aa1fa Re-add CUTSCENE_INSERT item with its own dedicated load buffer
Splicing a JSON-authored INSERT target by name previously resolved
through the same shared CUTSCENE_LOADED_ITEMS/POOL buffer as whatever
outer cutscene was currently running, so loading it would clobber the
outer cutscene's own items mid-playback - that's why it was reverted.
Gives INSERT its own smaller CUTSCENE_INSERT_ITEMS/POOL buffer and lets
the cutscene asset loader be told which buffer set to parse into, so an
INSERT's dynamic load no longer stomps the running cutscene.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-13 11:12:43 -05:00
YourWishes 49b97ba508 Switch cutscene system to a set of pre deffed buffers 2026-09-13 09:58:57 -05:00
YourWishes 6c11d8165d Revert "Add CUTSCENE_INSERT item to splice another cutscene's items in place"
This reverts commit 6af0e9cf28.
2026-09-13 09:29:23 -05:00
YourWishesandClaude Sonnet 5 93554b39ba Add DUSK_TRACK_MEMORY allocation tracker and memoryCheckUnfreed report
memoryAllocate() becomes a macro capturing __FILE__/__LINE__ into a
fixed-size tracking table when DUSK_TRACK_MEMORY is enabled (Linux only
for now), so a leak surviving to shutdown can be traced back to its call
site. Replaces engineDispose's hard assertTrue with memoryCheckUnfreed(),
which reports unfreed allocations (with call sites when tracking is on,
just a count otherwise) instead of aborting.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-13 09:19:24 -05:00
YourWishesandClaude Sonnet 5 6af0e9cf28 Add CUTSCENE_INSERT item to splice another cutscene's items in place
Unlike CUTSCENE (a one-way jump that replaces the running cutscene),
INSERT splices a referenced cutscene's items into the running one and
resumes right after itself once they run out. Backed by a small return
stack on CUTSCENE_SYSTEM so inserts can nest.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-13 08:56:26 -05:00
YourWishesandClaude Sonnet 5 083deb8357 Move cutscene item type enum and authoring macros to their own headers
cutsceneitemtype_t moves from cutsceneitem.h into cutsceneitembase.h,
alongside a new CUTSCENE_ITEM(TYPE, UNION_NAME, ...) shorthand that
fills in a cutsceneitem_t literal's .type and union member together.

Every CUTSCENE_* authoring macro (CUTSCENE_WAIT, CUTSCENE_TEXT,
CUTSCENE_AUDIO_PLAY, etc.) moves out of the single, ever-growing
cutscene.h into the header of the item type it actually authors,
built on top of CUTSCENE_ITEM. cutscene.h now only holds cutscene_t
itself plus the CUTSCENE/CUTSCENE_REFERENCE/CUTSCENE_CUTSCENE macros,
which aren't tied to any one item type. Every relocated macro also
gained a proper JSDoc-style comment block with @param tags.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 22:30:23 -05:00
YourWishesandClaude Sonnet 5 ffb375faeb Add battle abilities and wire them into START_BATTLE cutscene parsing
Adds a battleability_t enum (starting with FIRE) and a fixed
abilities[8] array on battlefighter_t. Extends the START_BATTLE
cutscene item to parse an optional per-enemy "abilities" JSON array
(via a new cutsceneJsonLookupBattleAbility name lookup) and copies
abilities onto every spawned fighter, from the party roster for
allies and from the parsed config for enemies.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 22:30:12 -05:00
YourWishesandClaude Sonnet 5 b8cbd8ff6a Make party and save-slot init/new-game operate on SAVE.slot directly
party.c's functions no longer take a party_t* - they always operate on
SAVE.slot.party, since gameplay only ever has one active party. That
forced the same change on saveSlotInit/saveSlotNewGame (they called
partyInit/partyAddMember internally), so those are now parameterless
too, always resetting/seeding SAVE.slot. Callers building a save at a
specific index (new-game creation, slot deletion) now set
SAVE.slotCurrent first instead of building an independent local struct -
safe since the select-save screen is only ever reached before any
gameplay starts.

saveSlotWriteJSON/saveSlotReadJSON deliberately keep their explicit
saveslot_t* parameter - they're genuine serialization primitives used
by the save-device layer and tests against arbitrary structs, unrelated
to the single-current-party assumption. saveSlotReadJSON's internal
reset is now a plain memset instead of delegating to the now-global-only
saveSlotInit().

Updates test_save/test_savedevice/test_savedevicelinux/test_saveslot for
the new signatures. Note: test_savedevice/test_savedevicelinux/test_save
have pre-existing failures in this sandbox unrelated to this change -
savetestfixture.c swaps $HOME, but saveDeviceLinuxGetDirectory actually
derives the save path from ASSET.baseDirectory (the executable's own
location), so the fixture's sandboxing never actually applies. test_
saveslot.c (which doesn't depend on that fixture) passes 15/15.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 21:30:08 -05:00
YourWishesandClaude Sonnet 5 c2bd761383 Remove testbattle - superseded by the JSON-driven battle cutscene
testBattleStart() was never actually called anywhere (its only trace
was a stray unused include in uimainmenu.c) - battletest.jsonc's
START_BATTLE/REGULAR_BATTLE flow replaces it.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 21:14:50 -05:00
YourWishesandClaude Sonnet 5 878e3438c8 Complete REGULAR_BATTLE immediately, fix stale saveslot tests
REGULAR_BATTLE previously never completed on its own, hanging any
cutscene that reached it - START_BATTLE already waits out the whole
encounter and returns to the overworld before this item ever runs, so
it's now a completes-immediately hook point for future regular-battle-
specific follow-up.

Also updates test_saveslot.c for the mapName/partyMembers schema
additions: the round-trip test now writes a valid map name for its
in-use slot, "missingFieldsUseDefaults" is replaced with a test for the
blank-slot short-circuit it was actually hitting, and a new regression
test locks in that an in-use slot with no map name is treated as corrupt.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 19:59:16 -05:00
YourWishesandClaude Sonnet 5 3da65370e0 Add requireStrMin save-JSON shorthand
Replaces the hand-rolled mapName missing/null/empty check in
saveSlotVer1ReadJSON with a reusable macro.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-12 19:55:17 -05:00
YourWishesandClaude Sonnet 5 83e646cba9 Load the save's map on select, seed a real new-game slot, fix readString off-by-one
New games now go through saveSlotNewGame (starting party + default map
name) instead of the empty saveSlotInit, and selecting a save loads its
stored map instead of the hardcoded boot-time overworld. Save loading now
requires an in-use slot to have a non-empty map name, treating a blank one
as corruption instead of silently faking a default.

Also fixes a real off-by-one in the readString save-JSON macro: it passed
its maxLength (documented as excluding the null terminator) straight
through as stringCopy's destSize, which needs +1 for the terminator - so a
string exactly at the length limit (e.g. "overworld", 9 chars) always
asserted "src is too long".

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-10 23:14:44 -05:00
YourWishesandClaude Sonnet 5 0566166af4 Persist party across saves and flag/warn on corrupt save slots
Party moves off a global singleton onto each save slot, letting
START_BATTLE pick specific active-order members to bring into a fight,
and round-trips through save/load via saveslotver1. A corrupt slot load
now flags the cache and shows a red "Corrupted Save" warning in the
select-save UI instead of silently resetting.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-10 14:19:03 -05:00
YourWishesandClaude Sonnet 5 ed0b8c3911 Exit delete mode on save select after delete or back button
Previously deleting a save left the panel in delete mode, and pressing
back while in delete mode closed the whole save-select screen instead
of just returning to the normal load view.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-09 21:38:28 -05:00
YourWishesandClaude Sonnet 5 03d753a83f Move cutscene JSON parsing entirely into the async loader phase
File read, yyjson parse, and all item dispatch now happen in
assetCutsceneLoaderAsync - none of it touches the main thread now that
CUTSCENE items defer their nested asset resolution to start time. The
sync phase is now just a state flip, kept only so assetUpdate()'s
onLoaded dispatch (which only fires for entries finishing via the sync
path) still runs.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-09 21:34:47 -05:00
YourWishes cf51ed1988 Fixed cutscene item cutscene trying to recursively load 2026-09-09 21:21:50 -05:00
YourWishesandClaude Sonnet 5 70f46e89d3 Merge branch 'raw-json-maps' into main
Load map chunks from raw JSON at runtime instead of a baked DCF/DMF
step, add a reusable perf timing module, fix a real PSP logDebug/logError
Memory Stick I/O cost bug, and switch chunk tile storage to a dense
minified grid.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-09 15:34:08 -05:00
160 changed files with 2680 additions and 1961 deletions

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
{
// Keyed by move id. "ability" matches a BATTLE_ABILITY_* enum name
// (see src/dusk/rpg/battle/battleability.h) - there's no JSON loader
// consuming this file yet, so field names/shape here are a starting
// point, not a contract.
"moves": {
"fire": {
"ability": "FIRE",
"power": 12,
"mpCost": 4,
"target": "SINGLE_ENEMY",
// Optional - name of a cutscene (see assets/cutscenes/) to play
// when this move is used, resolved lazily like a CUTSCENE item's
// "name" field. Omit for a move with no special presentation.
"cutscene": "battle/fire"
}
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"items": [
{
"type": "START_BATTLE",
"encounterType": "REGULAR",
"fleeAvailable": true,
"enemies": [
{
"stats": { "attack": 8, "defense": 4, "magic": 0, "speed": 5, "luck": 2 },
"healthMax": 30,
"mpMax": 0
},
{
"stats": { "attack": 6, "defense": 3, "magic": 0, "speed": 7, "luck": 2 },
"healthMax": 20,
"mpMax": 0
}
]
},
{
"type": "REGULAR_BATTLE"
}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"items": {
"potion": { "type": "MEDICINE" },
"potato": { "type": "FOOD" },
"apple": { "type": "FOOD" }
}
}
+6
View File
@@ -73,6 +73,12 @@ msgstr "Select Save"
msgid "ui.select_save.empty"
msgstr "Empty Slot"
msgid "ui.select_save.corrupt"
msgstr "Corrupted Save"
msgid "ui.select_save.corrupt_warning"
msgstr "This save file is corrupted and could not be loaded. Please contact support if this issue persists."
msgid "ui.select_save.slot_format"
msgstr "%s Lv.%d %s"
@@ -1 +1 @@
{"tiles":[[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[6,0],[2,0],[2,0],[2,0],[2,0],[2,0],[2,0],[7,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[8,0],[4,0],[4,0],[4,0],[12,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[8,0],[4,0],[4,0],[9,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0]],"meshes":[{"file":"house_5_3.dmf","pos":[1,1,0]}],"entities":[{"type":"global","globalId":3,"pos":[8,8,1]},{"type":"item","itemId":1,"quantity":1,"pos":[12,2,0]}],"areas":[{"min":[11,3,0],"max":[16,9,10],"callbackId":1,"notify":3,"trigger":6}]}
{"tiles":[[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],null,null,null,null,null,[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[6,0],[2,0],[2,0],[2,0],[2,0],[2,0],[2,0],[7,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[8,0],[4,0],[4,0],[4,0],[12,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[3,0],[1,1],[1,1],[5,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[8,0],[4,0],[4,0],[9,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0],[1,0]],"meshes":[{"file":"house_5_3.dmf","pos":[1,1,0]}],"entities":[{"type":"global","globalId":3,"pos":[8,8,1]},{"type":"global","globalId":4,"pos":[12,2,0]}],"areas":[{"min":[11,3,0],"max":[16,9,10],"callbackId":1,"notify":3,"trigger":6}]}
+1
View File
@@ -27,6 +27,7 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
)
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
set(DUSK_TRACK_MEMORY ON CACHE BOOL "Track memoryAllocate() call sites to help find memory leaks.")
# Define platform-specific macros.
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
+6
View File
@@ -50,6 +50,12 @@ if(DUSK_BACKTRACE)
)
endif()
if(DUSK_TRACK_MEMORY)
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_TRACK_MEMORY
)
endif()
# Includes
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
-1
View File
@@ -17,6 +17,5 @@ add_subdirectory(locale)
add_subdirectory(json)
add_subdirectory(chunk)
add_subdirectory(dmf)
add_subdirectory(cutscene)
add_subdirectory(wav)
add_subdirectory(mp3)
-6
View File
@@ -52,12 +52,6 @@ assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT] = {
.dispose = assetChunkDispose
},
[ASSET_LOADER_TYPE_CUTSCENE] = {
.loadSync = assetCutsceneLoaderSync,
.loadAsync = assetCutsceneLoaderAsync,
.dispose = assetCutsceneDispose
},
[ASSET_LOADER_TYPE_WAV] = {
.loadSync = assetWavLoaderSync,
.loadAsync = assetWavLoaderAsync,
-5
View File
@@ -13,7 +13,6 @@
#include "asset/loader/locale/assetlocaleloader.h"
#include "asset/loader/json/assetjsonloader.h"
#include "asset/loader/chunk/assetchunkloader.h"
#include "asset/loader/cutscene/assetcutsceneloader.h"
#include "asset/loader/wav/assetwavloader.h"
#include "asset/loader/mp3/assetmp3loader.h"
@@ -27,7 +26,6 @@ typedef enum {
ASSET_LOADER_TYPE_LOCALE,
ASSET_LOADER_TYPE_JSON,
ASSET_LOADER_TYPE_CHUNK,
ASSET_LOADER_TYPE_CUTSCENE,
ASSET_LOADER_TYPE_WAV,
ASSET_LOADER_TYPE_MP3,
@@ -42,7 +40,6 @@ typedef union {
assetlocaleloaderloading_t locale;
assetjsonloaderloading_t json;
assetchunkloaderloading_t chunk;
assetcutsceneloaderloading_t cutscene;
assetwavloaderloading_t wav;
assetmp3loaderloading_t mp3;
} assetloaderloading_t;
@@ -55,7 +52,6 @@ typedef union {
assetlocaleoutput_t locale;
assetjsonoutput_t json;
assetchunkoutput_t chunk;
assetcutsceneoutput_t cutscene;
assetwavoutput_t wav;
assetmp3output_t mp3;
} assetloaderoutput_t;
@@ -66,7 +62,6 @@ typedef union {
assetlocaleloaderinput_t locale;
assetjsonloaderinput_t json;
assetchunkloaderinput_t chunk;
assetcutsceneloaderinput_t cutscene;
assetwavloaderinput_t wav;
assetmp3loaderinput_t mp3;
} assetloaderinput_t;
@@ -1,9 +0,0 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
assetcutsceneloader.c
)
@@ -1,225 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assetcutsceneloader.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "util/string.h"
#include "asset/loader/assetloading.h"
#include "asset/loader/assetentry.h"
#include "asset/loader/assetloader.h"
#include "asset/asset.h"
#include "rpg/cutscene/item/json/cutscenejsonpauseflags.h"
errorret_t assetCutsceneParseItem(
assetloading_t *loading,
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset,
const uint8_t index
) {
if(!yyjson_is_obj(itemObj)) {
assetLoaderErrorThrow(
loading,
"Cutscene item %u is not an object",
(uint32_t)index
);
}
const char_t *typeName = yyjson_get_str(yyjson_obj_get(itemObj, "type"));
if(!cutsceneItemLookupType(typeName, &item->type)) {
assetLoaderErrorThrow(
loading,
"Cutscene item %u: missing/unsupported item type %s",
(uint32_t)index,
typeName
);
}
cutsceneitemloadcallback_t *load = CUTSCENE_ITEM_CALLBACKS[item->type].load;
if(load == NULL) {
assetLoaderErrorThrow(
loading, "Cutscene item %u: unhandled item type %u", (uint32_t)index,
(uint32_t)item->type
);
}
errorret_t result = load(itemObj, item, pool, poolOffset);
if(errorIsNotOk(result)) {
loading->entry->state = ASSET_ENTRY_STATE_ERROR;
errorChain(result);
}
errorOk();
}
size_t assetCutsceneComputePoolSize(yyjson_val *itemsArr) {
size_t size = 0;
yyjson_arr_iter iter = yyjson_arr_iter_with(itemsArr);
yyjson_val *itemVal;
while((itemVal = yyjson_arr_iter_next(&iter)) != NULL) {
const char_t *typeName = yyjson_get_str(yyjson_obj_get(itemVal, "type"));
cutsceneitemtype_t type;
if(!cutsceneItemLookupType(typeName, &type)) continue;
cutsceneitempoolsizecallback_t *poolSize =
CUTSCENE_ITEM_CALLBACKS[type].poolSize;
if(poolSize == NULL) continue;
size = cutsceneJsonPoolAlign(size) + poolSize(itemVal);
}
return size;
}
void assetCutsceneFreeParsed(assetcutsceneoutput_t *out) {
if(out->items != NULL) {
memoryFree(out->items);
out->items = NULL;
}
if(out->pool != NULL) {
memoryFree(out->pool);
out->pool = NULL;
}
if(out->doc != NULL) {
yyjson_doc_free(out->doc);
out->doc = NULL;
}
}
errorret_t assetCutsceneLoaderAsync(assetloading_t *loading) {
assertNotNull(loading, "Loading cannot be NULL");
assertNotMainThread("Should be called from an async thread.");
if(
loading->loading.cutscene.state != ASSET_CUTSCENE_LOADING_STATE_READ_FILE
) {
errorOk();
}
assertNull(loading->loading.cutscene.buffer, "Buffer already defined?");
assetfile_t *file = &loading->loading.cutscene.file;
assetLoaderErrorChain(loading,
assetFileInit(file, loading->entry->name, NULL, NULL)
);
if(file->size > ASSET_CUTSCENE_FILE_SIZE_MAX) {
assetLoaderErrorThrow(
loading, "Cutscene JSON exceeds maximum allowed size"
);
}
size_t fileSize = (size_t)file->size;
uint8_t *buffer = memoryAllocate(fileSize);
assetLoaderErrorChain(loading, assetFileOpen(file));
assetLoaderErrorChain(loading, assetFileRead(file, buffer, fileSize));
assertTrue(
file->lastRead == file->size,
"Failed to read entire cutscene file."
);
assetLoaderErrorChain(loading, assetFileClose(file));
assetLoaderErrorChain(loading, assetFileDispose(file));
loading->loading.cutscene.buffer = buffer;
loading->loading.cutscene.size = fileSize;
loading->loading.cutscene.state = ASSET_CUTSCENE_LOADING_STATE_PARSE;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
}
errorret_t assetCutsceneLoaderSync(assetloading_t *loading) {
assertNotNull(loading, "Loading cannot be NULL");
assertTrue(loading->type == ASSET_LOADER_TYPE_CUTSCENE, "Invalid type.");
assertIsMainThread("Must be called from the main thread.");
if(loading->loading.cutscene.state == ASSET_CUTSCENE_LOADING_STATE_INITIAL) {
loading->loading.cutscene.state = ASSET_CUTSCENE_LOADING_STATE_READ_FILE;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
errorOk();
}
assetcutsceneoutput_t *out = &loading->entry->data.cutscene;
uint8_t *buffer = loading->loading.cutscene.buffer;
assertNotNull(buffer, "Cutscene data should have been loaded by now.");
size_t bufferSize = loading->loading.cutscene.size;
yyjson_doc *doc = yyjson_read(
(char *)buffer, bufferSize,
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
);
memoryFree(buffer);
loading->loading.cutscene.buffer = NULL;
if(doc == NULL) {
assetLoaderErrorThrow(loading, "Failed to parse cutscene JSON");
}
out->doc = doc;
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *itemsArr = yyjson_obj_get(root, "items");
if(!yyjson_is_arr(itemsArr)) {
assetCutsceneFreeParsed(out);
assetLoaderErrorThrow(loading, "Cutscene JSON is missing an 'items' array");
}
size_t itemCount = yyjson_arr_size(itemsArr);
if(itemCount > UINT8_MAX) {
assetCutsceneFreeParsed(out);
assetLoaderErrorThrow(loading, "Cutscene has more than 255 items");
}
cutscenepause_t pause = CUTSCENE_PAUSE_DEFAULT;
yyjson_val *pauseArr = yyjson_obj_get(root, "pause");
if(yyjson_is_arr(pauseArr)) {
if(!cutsceneJsonParsePauseFlags(pauseArr, &pause)) {
assetCutsceneFreeParsed(out);
assetLoaderErrorThrow(
loading, "Cutscene JSON has an invalid 'pause' flag"
);
}
}
out->items = memoryAllocate(itemCount * sizeof(cutsceneitem_t));
memoryZero(out->items, itemCount * sizeof(cutsceneitem_t));
size_t poolSize = assetCutsceneComputePoolSize(itemsArr);
out->pool = poolSize > 0 ? memoryAllocate(poolSize) : NULL;
size_t poolOffset = 0;
yyjson_arr_iter itemIter = yyjson_arr_iter_with(itemsArr);
yyjson_val *itemVal;
uint8_t index = 0;
while((itemVal = yyjson_arr_iter_next(&itemIter)) != NULL) {
errorret_t itemResult = assetCutsceneParseItem(
loading, itemVal, &out->items[index], out->pool, &poolOffset, index
);
if(errorIsNotOk(itemResult)) {
assetCutsceneFreeParsed(out);
errorChain(itemResult);
}
index++;
}
out->cutscene.items = out->items;
out->cutscene.itemCount = (uint8_t)itemCount;
out->cutscene.pause = pause;
out->cutscene.dataSize = 0;
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
errorOk();
}
errorret_t assetCutsceneDispose(assetentry_t *entry) {
assertNotNull(entry, "Entry cannot be NULL");
assertTrue(entry->type == ASSET_LOADER_TYPE_CUTSCENE, "Invalid type.");
assertIsMainThread("Must be called from the main thread.");
assetCutsceneFreeParsed(&entry->data.cutscene);
errorOk();
}
@@ -1,106 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "asset/assetfile.h"
#include "rpg/cutscene/cutscene.h"
#include "rpg/cutscene/item/cutsceneitem.h"
#include "rpg/cutscene/item/json/cutscenejsonpool.h"
#include "yyjson.h"
#define ASSET_CUTSCENE_FILE_SIZE_MAX (1024 * 64)
typedef struct assetloading_s assetloading_t;
typedef struct assetentry_s assetentry_t;
typedef struct {
void *nothing;
} assetcutsceneloaderinput_t;
typedef enum {
ASSET_CUTSCENE_LOADING_STATE_INITIAL,
ASSET_CUTSCENE_LOADING_STATE_READ_FILE,
ASSET_CUTSCENE_LOADING_STATE_PARSE
} assetcutsceneloadingstate_t;
typedef struct {
assetfile_t file;
assetcutsceneloadingstate_t state;
uint8_t *buffer;
size_t size;
} assetcutsceneloaderloading_t;
typedef struct {
cutscene_t cutscene;
cutsceneitem_t *items;
uint8_t *pool;
yyjson_doc *doc;
} assetcutsceneoutput_t;
/**
* Parses one cutscene item object into item, writing any pool-backed
* array data (entityWalkTo positions, mapAreaWait areaIds) into pool at
* *poolOffset and advancing it. Every field/enum name is validated as
* real (untrusted, authored) file content - failures are real errors, not
* asserts. Dispatches to the matching type's Load callback (see
* CUTSCENE_ITEM_CALLBACKS in rpg/cutscene/item/cutsceneitem.h) - the
* per-type parsing logic itself lives there now, colocated with that
* type's runtime behavior.
*
* @param loading Loading information for the asset being loaded.
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Base pointer of the entry's pool allocation (may be NULL if
* assetCutsceneComputePoolSize returned 0).
* @param poolOffset In/out cursor into pool.
* @param index Index of this item within the cutscene, for error messages.
* @return Error code indicating success or failure of the parse.
*/
errorret_t assetCutsceneParseItem(
assetloading_t *loading,
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset,
const uint8_t index
);
/**
* Frees whatever of out->items/out->pool/out->doc is currently non-NULL
* and clears them - shared by both the normal disposer and every parse
* failure path's cleanup.
*
* @param out The output to free.
*/
void assetCutsceneFreeParsed(assetcutsceneoutput_t *out);
/**
* Asynchronous loader for cutscene assets. Reads the raw JSONC file bytes
* into the loading buffer so the sync phase can parse without blocking the
* main thread on I/O.
*
* @param loading Loading information for the asset being loaded.
* @return Error code indicating success or failure of the load operation.
*/
errorret_t assetCutsceneLoaderAsync(assetloading_t *loading);
/**
* Synchronous loader for cutscene assets. Parses the JSONC previously read
* by the async phase into a heap-allocated cutsceneitem_t array + pool.
*
* @param loading Loading information for the asset being loaded.
* @return Error code indicating success or failure of the load operation.
*/
errorret_t assetCutsceneLoaderSync(assetloading_t *loading);
/**
* Disposer for cutscene assets.
*
* @param entry Asset entry containing the cutscene data to dispose.
* @return Error code indicating success or failure of the dispose operation.
*/
errorret_t assetCutsceneDispose(assetentry_t *entry);
+4 -5
View File
@@ -12,11 +12,10 @@
// Forward-declared rather than including asset/asset.h - this header only
// ever needs a pointer to it, and pulling in the full asset module here
// creates a real include cycle for anything that reaches this header via
// asset/loader/cutscene/assetcutsceneloader.h (asset.h -> ... ->
// assetloader.h -> assetcutsceneloader.h -> cutscene.h -> cutsceneitem.h
// -> the audio cutscene items -> this header -> asset/asset.h again,
// mid-parse). audiomixerchannel.c itself still includes the full header.
// risks an include cycle for anything that reaches this header via a
// cutscene item (cutscene.h -> cutsceneitem.h -> the audio cutscene items
// -> this header -> asset/asset.h, mid-parse). audiomixerchannel.c itself
// still includes the full header.
typedef struct assetentry_s assetentry_t;
typedef struct audiostream_s audiostream_t;
+3 -1
View File
@@ -47,8 +47,8 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(localeManagerInit());
errorChain(displayInit());
errorChain(audioInit());
errorChain(uiInit());
errorChain(rpgInit());
errorChain(uiInit());
#ifdef DUSK_NETWORK
errorChain(networkInit());
#endif
@@ -108,5 +108,7 @@ errorret_t engineDispose(void) {
errorChain(assetDispose());
perfDispose();
memoryCheckUnfreed();
errorOk();
}
-3
View File
@@ -10,6 +10,3 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
battlefighter.c
party.c
)
# Subdirs
add_subdirectory(testbattle)
+1 -17
View File
@@ -7,6 +7,7 @@
#pragma once
#include "battlefighter.h"
#include "battleaction.h"
#define BATTLE_FIGHTER_COUNT_MAX 8
@@ -46,28 +47,11 @@ typedef enum {
BATTLE_STATE_COUNT
} battlestate_t;
typedef enum {
BATTLE_ACTION_NONE, // No action decided yet for this fighter this round.
BATTLE_ACTION_ATTACK,
BATTLE_ACTION_COUNT
} battleactiontype_t;
typedef struct {
battleactiontype_t type;
uint8_t targetIndex; // Meaningful for BATTLE_ACTION_ATTACK.
} battleaction_t;
typedef void (*battlestatechangedcallback_t)(
const battlestate_t previous,
const battlestate_t next
);
typedef void (*battleactiondecidedcallback_t)(
const battlefighter_t *fighter,
const battleaction_t *action
);
typedef struct {
battlestate_t state;
battlefighter_t fighters[BATTLE_FIGHTER_COUNT_MAX];
+17
View File
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
typedef enum {
BATTLE_ABILITY_NULL,
BATTLE_ABILITY_FIRE,
BATTLE_ABILITY_COUNT
} battleability_t;
+27
View File
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "battlefighter.h"
typedef enum {
BATTLE_ACTION_NONE, // No action decided yet for this fighter this round.
BATTLE_ACTION_ATTACK,
BATTLE_ACTION_COUNT
} battleactiontype_t;
typedef struct {
battleactiontype_t type;
uint8_t targetIndex; // Meaningful for BATTLE_ACTION_ATTACK.
} battleaction_t;
typedef void (*battleactiondecidedcallback_t)(
const battlefighter_t *fighter,
const battleaction_t *action
);
+4
View File
@@ -7,6 +7,9 @@
#pragma once
#include "error/error.h"
#include "battleability.h"
#define BATTLE_FIGHTER_ABILITY_COUNT_MAX 8
// An empty status means the slot in BATTLE.fighters is unused.
typedef enum {
@@ -55,6 +58,7 @@ typedef struct {
uint16_t mpMax;
battlefighterstats_t stats;
battleability_t abilities[BATTLE_FIGHTER_ABILITY_COUNT_MAX];
} battlefighter_t;
/**
+18 -12
View File
@@ -8,22 +8,25 @@
#include "party.h"
#include "assert/assert.h"
#include "util/memory.h"
party_t PARTY;
#include "save/save.h"
void partyInit(void) {
memoryZero(&PARTY, sizeof(party_t));
party_t *party = &SAVE.slot.party;
memoryZero(party, sizeof(party_t));
for(uint8_t i = 0; i < PARTY_MEMBER_COUNT_MAX; i++) {
PARTY.members[i].id = i;
party->members[i].id = i;
}
for(uint8_t i = 0; i < PARTY_ACTIVE_SIZE_MAX; i++) {
PARTY.order[i] = PARTY_ORDER_EMPTY;
party->order[i] = PARTY_ORDER_EMPTY;
}
}
uint8_t partyGetAvailableMember(void) {
const party_t *party = &SAVE.slot.party;
for(uint8_t i = 0; i < PARTY_MEMBER_COUNT_MAX; i++) {
if(PARTY.members[i].status == BATTLE_FIGHTER_STATUS_NULL) return i;
if(party->members[i].status == BATTLE_FIGHTER_STATUS_NULL) return i;
}
return 0xFF;
@@ -34,18 +37,20 @@ battlefighter_t *partyAddMember(
const uint16_t healthMax,
const uint16_t mpMax
) {
party_t *party = &SAVE.slot.party;
const uint8_t index = partyGetAvailableMember();
if(index == 0xFF) return NULL;
battlefighter_t *member = &PARTY.members[index];
battlefighter_t *member = &party->members[index];
battleFighterInit(
member, BATTLE_FIGHTER_TEAM_ALLY, BATTLE_FIGHTER_CONTROLLER_PLAYER,
stats, healthMax, mpMax
);
for(uint8_t i = 0; i < PARTY_ACTIVE_SIZE_MAX; i++) {
if(PARTY.order[i] != PARTY_ORDER_EMPTY) continue;
PARTY.order[i] = index;
if(party->order[i] != PARTY_ORDER_EMPTY) continue;
party->order[i] = index;
break;
}
@@ -55,9 +60,10 @@ battlefighter_t *partyAddMember(
battlefighter_t *partyGetOrderMember(const uint8_t slot) {
assertTrue(slot < PARTY_ACTIVE_SIZE_MAX, "Invalid party order slot");
const uint8_t index = PARTY.order[slot];
party_t *party = &SAVE.slot.party;
const uint8_t index = party->order[slot];
if(index == PARTY_ORDER_EMPTY) return NULL;
return &PARTY.members[index];
return &party->members[index];
}
void partySetOrder(const uint8_t slot, const uint8_t memberIndex) {
@@ -67,5 +73,5 @@ void partySetOrder(const uint8_t slot, const uint8_t memberIndex) {
"Invalid party member index"
);
PARTY.order[slot] = memberIndex;
SAVE.slot.party.order[slot] = memberIndex;
}
+13 -10
View File
@@ -22,24 +22,25 @@ typedef struct {
uint8_t order[PARTY_ACTIVE_SIZE_MAX];
} party_t;
extern party_t PARTY;
/**
* Initializes the party system with an empty roster and order.
* Initializes the current save slot's party (SAVE.slot.party) with an
* empty roster and order.
*/
void partyInit(void);
/**
* Gets an available (unused) party member slot index.
* Gets an available (unused) member slot index in the current save slot's
* party.
*
* @return The index of an available slot, or 0xFF if the party is full.
*/
uint8_t partyGetAvailableMember(void);
/**
* Adds a member to the party roster in the next available slot. Party
* members are always allies controlled by the player. If there is a
* free active order slot, the new member is placed into it.
* Adds a member to the current save slot's party roster in the next
* available slot. Party members are always allies controlled by the
* player. If there is a free active order slot, the new member is placed
* into it.
*
* @param stats The member's base combat stats.
* @param healthMax The member's maximum health.
@@ -54,7 +55,8 @@ battlefighter_t *partyAddMember(
);
/**
* Gets the roster member currently occupying an active order slot.
* Gets the roster member currently occupying an active order slot in the
* current save slot's party.
*
* @param slot The active order slot to query.
* @return Pointer to the member in that slot, or NULL if the slot is
@@ -63,8 +65,9 @@ battlefighter_t *partyAddMember(
battlefighter_t *partyGetOrderMember(const uint8_t slot);
/**
* Assigns a roster member to an active order slot, replacing whatever
* was there. Use PARTY_ORDER_EMPTY to clear a slot.
* Assigns a roster member to an active order slot in the current save
* slot's party, replacing whatever was there. Use PARTY_ORDER_EMPTY to
* clear a slot.
*
* @param slot The active order slot to assign.
* @param memberIndex The roster member index to place there, or
@@ -1,10 +0,0 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
testbattle.c
)
@@ -1,43 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "testbattle.h"
#include "rpg/battle/battle.h"
#include "scene/scene.h"
void testBattleStart(void) {
battleInit();
const battlefighterstats_t allyOneStats =
{ .attack = 10, .defense = 5, .magic = 0, .speed = 10, .luck = 0 };
const battlefighterstats_t allyTwoStats =
{ .attack = 8, .defense = 4, .magic = 0, .speed = 8, .luck = 0 };
const battlefighterstats_t enemyOneStats =
{ .attack = 6, .defense = 3, .magic = 0, .speed = 6, .luck = 0 };
const battlefighterstats_t enemyTwoStats =
{ .attack = 7, .defense = 3, .magic = 0, .speed = 5, .luck = 0 };
battleAddFighter(
BATTLE_FIGHTER_TEAM_ALLY, BATTLE_FIGHTER_CONTROLLER_PLAYER,
allyOneStats, 30, 10
);
battleAddFighter(
BATTLE_FIGHTER_TEAM_ALLY, BATTLE_FIGHTER_CONTROLLER_PLAYER,
allyTwoStats, 25, 10
);
battleAddFighter(
BATTLE_FIGHTER_TEAM_ENEMY, BATTLE_FIGHTER_CONTROLLER_AI,
enemyOneStats, 20, 5
);
battleAddFighter(
BATTLE_FIGHTER_TEAM_ENEMY, BATTLE_FIGHTER_CONTROLLER_AI,
enemyTwoStats, 20, 5
);
battleStart(BATTLE_ENCOUNTER_REGULAR, true);
sceneSet(SCENE_TYPE_BATTLE);
}
@@ -1,16 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
/**
* TEMPORARY test hook: sets up a hardcoded mock battle and switches to the
* battle scene, so the battle scene (camera/fighters/HUD) can be seen and
* played without a real encounter trigger yet.
*/
void testBattleStart(void);
+1
View File
@@ -6,6 +6,7 @@
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
cutscene.c
cutscenesystem.c
)
+145
View File
@@ -0,0 +1,145 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "cutscene.h"
#include "item/json/cutscenejsonpauseflags.h"
#include "util/memory.h"
#include "assert/assert.h"
errorret_t cutsceneParseItem(
yyjson_val *itemObj,
cutsceneitem_t *item,
const uint8_t index
) {
assertNotNull(itemObj, "Item object cannot be NULL");
assertNotNull(item, "Item cannot be NULL");
if(!yyjson_is_obj(itemObj)) {
errorThrow("Cutscene item %u is not an object", (uint32_t)index);
}
const char_t *typeName = yyjson_get_str(yyjson_obj_get(itemObj, "type"));
if(!cutsceneItemLookupType(typeName, &item->type)) {
errorThrow(
"Cutscene item %u: missing/unsupported item type %s",
(uint32_t)index, typeName
);
}
cutsceneitemloadcallback_t *load = CUTSCENE_ITEM_CALLBACKS[item->type].load;
if(load == NULL) {
errorThrow(
"Cutscene item %u: unhandled item type %u",
(uint32_t)index, (uint32_t)item->type
);
}
errorret_t result = load(itemObj, item);
if(errorIsNotOk(result)) errorChain(result);
errorOk();
}
size_t cutsceneCountItems(yyjson_doc *doc) {
assertNotNull(doc, "Doc cannot be NULL");
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *itemsArr = yyjson_obj_get(root, "items");
if(!yyjson_is_arr(itemsArr)) return 0;
return yyjson_arr_size(itemsArr);
}
errorret_t cutsceneParseDoc(
yyjson_doc *doc,
cutscene_t *scene,
cutsceneitem_t *items,
size_t itemsMax
) {
assertNotNull(doc, "Doc cannot be NULL");
assertNotNull(scene, "Scene cannot be NULL");
assertNotNull(items, "Items cannot be NULL");
assertTrue(itemsMax > 0, "Items max cannot be 0");
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *itemsArr = yyjson_obj_get(root, "items");
if(!yyjson_is_arr(itemsArr)) {
errorThrow("Cutscene JSON is missing an 'items' array");
}
size_t itemCount = yyjson_arr_size(itemsArr);
if(itemCount > itemsMax) {
errorThrow("Cutscene has more items than its target buffer allows");
}
cutscenepause_t pause = CUTSCENE_PAUSE_DEFAULT;
yyjson_val *pauseArr = yyjson_obj_get(root, "pause");
if(yyjson_is_arr(pauseArr)) {
if(!cutsceneJsonParsePauseFlags(pauseArr, &pause)) {
errorThrow("Cutscene JSON has an invalid 'pause' flag");
}
}
// Everything structural has validated by this point, so it's safe to
// start overwriting items - a per-item parse failure past this point
// still leaves it partially overwritten, but that's fine: any load
// failure here already sends the game into the fatal-error overlay
// (see cutsceneSystemLoad/cutsceneCutsceneResolve/cutsceneInsertResolve),
// so nothing keeps running against stale data.
memoryZero(items, itemCount * sizeof(cutsceneitem_t));
yyjson_arr_iter itemIter = yyjson_arr_iter_with(itemsArr);
yyjson_val *itemVal;
uint8_t index = 0;
while((itemVal = yyjson_arr_iter_next(&itemIter)) != NULL) {
errorret_t itemResult = cutsceneParseItem(itemVal, &items[index], index);
if(errorIsNotOk(itemResult)) errorChain(itemResult);
index++;
}
scene->itemCount = (uint8_t)itemCount;
scene->pause = pause;
errorOk();
}
void cutsceneAppend(
cutscene_t *scene,
const size_t itemsMax,
const cutsceneitem_t *items,
const uint8_t count
) {
assertNotNull(scene, "Scene cannot be NULL");
cutsceneAppendNext(scene, itemsMax, scene->itemCount, items, count);
}
void cutsceneAppendNext(
cutscene_t *scene,
const size_t itemsMax,
const uint8_t atIndex,
const cutsceneitem_t *items,
const uint8_t count
) {
assertNotNull(scene, "Scene cannot be NULL");
assertNotNull(items, "Items cannot be NULL");
assertTrue(count > 0, "Count cannot be 0");
assertTrue(atIndex <= scene->itemCount, "atIndex out of bounds");
assertTrue(
(size_t)scene->itemCount + count <= itemsMax,
"Cutscene item append exceeds itemsMax"
);
const size_t tailCount = (size_t)scene->itemCount - atIndex;
if(tailCount > 0) {
memoryMove(
&scene->items[atIndex + count],
&scene->items[atIndex],
tailCount * sizeof(cutsceneitem_t)
);
}
memoryCopy(&scene->items[atIndex], items, count * sizeof(cutsceneitem_t));
scene->itemCount += count;
}
+87 -494
View File
@@ -1,6 +1,6 @@
/**
* Copyright (c) 2025 Dominic Masters
*
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
@@ -10,504 +10,97 @@
#include "rpg/cutscene/cutscenepause.h"
typedef struct cutscene_s {
const cutsceneitem_t *items;
cutsceneitem_t *items;
uint8_t itemCount;
cutscenepause_t pause;
// Size in bytes of this cutscene's custom user data, carved out of
// CUTSCENE_SYSTEM.data while the cutscene is running.
size_t dataSize;
} cutscene_t;
#define CUTSCENE(NAME, SIZE, PAUSE_TYPE, ...) \
static const cutsceneitem_t CUTSCENE_##NAME##_ITEMS[] = { __VA_ARGS__ }; \
static const cutscene_t CUTSCENE_##NAME = { \
.items = CUTSCENE_##NAME##_ITEMS, \
.itemCount = sizeof(CUTSCENE_##NAME##_ITEMS) / sizeof(cutsceneitem_t), \
.pause = CUTSCENE_PAUSE_##PAUSE_TYPE, \
.dataSize = SIZE \
};
/**
* Parses one cutscene item object into item. Every field/enum name is
* validated as real (untrusted, authored) file content - failures are
* real errors, not asserts. Dispatches to the matching type's Load
* callback (see CUTSCENE_ITEM_CALLBACKS in item/cutsceneitem.h) - the
* per-type parsing logic itself lives there, colocated with that type's
* runtime behavior.
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param index Index of this item within the cutscene, for error messages.
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneParseItem(
yyjson_val *itemObj,
cutsceneitem_t *item,
const uint8_t index
);
#define CUTSCENE_REFERENCE(CUTSCENE) \
&CUTSCENE_##CUTSCENE
/**
* Counts how many entries are in doc's top-level "items" array, without
* parsing any of them - e.g. so a caller that wants to malloc its own
* items array instead of using a fixed one could size it first (we don't
* do that ourselves, but the option stays open).
*
* @param doc Already-parsed JSON document.
* @return Number of entries in "items", or 0 if it's missing/not an array.
*/
size_t cutsceneCountItems(yyjson_doc *doc);
#define CUTSCENE_TEXT(TEXT) \
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = { .text = TEXT } }
/**
* Parses doc's "items" (and optional "pause") into items, and updates
* scene's itemCount/pause - scene->items is left untouched, since the
* caller already points it at items. Purely a parser: doesn't touch the
* asset system, doesn't lock/load anything, and doesn't take ownership of
* doc - the caller is responsible for both loading doc beforehand and for
* its lifetime afterward (name/string fields written into items point
* directly into it, so it must outlive items' use).
*
* @param doc Already-parsed JSON document to read from.
* @param scene Destination scene - itemCount/pause are overwritten.
* @param items Destination item array, capacity itemsMax.
* @param itemsMax Capacity of items.
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneParseDoc(
yyjson_doc *doc,
cutscene_t *scene,
cutsceneitem_t *items,
size_t itemsMax
);
#define CUTSCENE_TEXT_MINI(TEXT, X, Y, Z, DURATION) \
{ \
.type = CUTSCENE_ITEM_TYPE_TEXT_MINI, \
.textMini = { \
.text = TEXT, \
.position = { X, Y, Z }, \
.duration = DURATION \
} \
}
/**
* Appends items onto the end of scene->items, growing scene->itemCount.
* scene->items must point at a buffer with capacity itemsMax - asserts if
* appending would exceed it.
*
* @param scene Cutscene to append to.
* @param itemsMax Capacity of scene->items.
* @param items Items to append, copied in order.
* @param count Number of entries in items.
*/
void cutsceneAppend(
cutscene_t *scene,
const size_t itemsMax,
const cutsceneitem_t *items,
const uint8_t count
);
#define CUTSCENE_TEXT_MINI_HIDE(INDEX) \
{ \
.type = CUTSCENE_ITEM_TYPE_TEXT_MINI_HIDE, \
.textMiniHide = { .index = INDEX } \
}
#define CUTSCENE_WAIT(WAIT) \
{ .type = CUTSCENE_ITEM_TYPE_WAIT, .wait = WAIT }
// A named, otherwise no-op position in the item list that cutsceneGoTo
// can jump execution straight to. NAME is matched with stringEquals,
// not pointer identity, so it's safe to use separate string literals
// with the same contents at the marker and at each call site.
#define CUTSCENE_MARKER(NAME) \
{ .type = CUTSCENE_ITEM_TYPE_MARKER, .marker = { .name = NAME } }
// Restarts the currently running cutscene from its first item,
// preserving whatever interact/interacted entities triggered it.
#define CUTSCENE_RESTART() \
{ .type = CUTSCENE_ITEM_TYPE_RESTART }
// Blocks the cutscene here indefinitely - it never completes on its own,
// only cutsceneGoTo (called externally, e.g. from a UI callback) can move
// execution past it.
#define CUTSCENE_IDLE() \
{ .type = CUTSCENE_ITEM_TYPE_IDLE }
// Opens a full-screen UI panel (e.g. the main menu) declaratively, then
// immediately continues on to whatever follows this item.
#define CUTSCENE_UI_SHOW(SCREEN) \
{ .type = CUTSCENE_ITEM_TYPE_UI_SHOW, .uiShow = { .screen = SCREEN } }
// Requests a switch to a different SCENE_TYPE via sceneSet, then
// immediately continues on to whatever follows this item - the switch
// itself doesn't happen until the next sceneUpdate() tick, so it does
// not take effect this frame.
#define CUTSCENE_SCENE(TYPE) \
{ .type = CUTSCENE_ITEM_TYPE_SCENE, .sceneChange = { .type = TYPE } }
#define CUTSCENE_CUTSCENE(CUTSCENE) \
{ \
.type = CUTSCENE_ITEM_TYPE_CUTSCENE, \
.cutscene = CUTSCENE_REFERENCE(CUTSCENE) \
}
#define CUTSCENE_CALLBACK(CALLBACK) \
{ .type = CUTSCENE_ITEM_TYPE_CALLBACK, .callback = CALLBACK }
#define CUTSCENE_PRINT(TEXT) \
{ .type = CUTSCENE_ITEM_TYPE_PRINT, .print = { .text = TEXT } }
// Shows a message-only modal (no option buttons) and immediately
// continues on to whatever follows this item - it does not wait for
// the dialog to be dismissed. Script the rest of the interaction (e.g.
// CUTSCENE_CALLBACK to kick off work, CUTSCENE_WAIT, then
// CUTSCENE_MODAL_CLOSE) as later items in the same cutscene.
// TITLE and MESSAGE are each displayed as-is unless they match a
// locale message ID, in which case the translated string is shown
// instead - see uiModalLocalize.
#define CUTSCENE_MODAL(TITLE, MESSAGE) \
{ \
.type = CUTSCENE_ITEM_TYPE_MODAL, \
.modal = { .title = TITLE, .message = MESSAGE } \
}
// Shows a modal with option buttons and immediately continues on, same
// as CUTSCENE_MODAL - it does not block waiting for a selection.
// Back/cancel input is disabled while it's open (see
// uiMenuSetDisableBack), so it must be dismissed by picking one; CALLBACK
// then fires with the selected option index once the dialog closes.
// Option labels are passed as trailing arguments, e.g.
// CUTSCENE_MODAL_OPTIONS(title, message, callback, "Retry", "Cancel") -
// their strings are not copied by this item, so they must stay valid
// until the modal opens (string literals are fine). Like TITLE and
// MESSAGE, each option is translated if it matches a locale message ID.
#define CUTSCENE_MODAL_OPTIONS(TITLE, MESSAGE, CALLBACK, ...) \
{ \
.type = CUTSCENE_ITEM_TYPE_MODAL, \
.modal = { \
.title = TITLE, .message = MESSAGE, \
.options = (const char_t *[]){ __VA_ARGS__ }, \
.optionCount = (uint8_t)( \
sizeof((const char_t *[]){ __VA_ARGS__ }) / sizeof(const char_t *) \
), \
.callback = CALLBACK \
} \
}
// Same as CUTSCENE_MODAL_OPTIONS, but fixed to exactly one option and
// MARKER1 to jump straight to via cutsceneGoTo once it's selected - no
// callback function to write. Does not fall through to whatever follows
// this item, so MARKER1 must be scripted elsewhere in the same cutscene.
#define CUTSCENE_MODAL_OPTIONS_ONE(TITLE, MESSAGE, OPTION1, MARKER1) \
{ \
.type = CUTSCENE_ITEM_TYPE_MODAL_OPTIONS_MARKERS, \
.modalOptionsMarkers = { \
.title = TITLE, .message = MESSAGE, \
.options = { OPTION1 }, \
.markers = { MARKER1 }, \
.optionCount = 1 \
} \
}
// Same as CUTSCENE_MODAL_OPTIONS, but fixed to exactly two options,
// jumping straight to OPTION1_MARKER or OPTION2_MARKER via cutsceneGoTo
// once the corresponding option is selected - no callback function to
// write. Does not fall through to whatever follows this item, so both
// markers must be scripted elsewhere in the same cutscene.
#define CUTSCENE_MODAL_OPTIONS_TWO( \
TITLE, MESSAGE, OPTION1, OPTION1_MARKER, OPTION2, OPTION2_MARKER \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_MODAL_OPTIONS_MARKERS, \
.modalOptionsMarkers = { \
.title = TITLE, .message = MESSAGE, \
.options = { OPTION1, OPTION2 }, \
.markers = { OPTION1_MARKER, OPTION2_MARKER }, \
.optionCount = 2 \
} \
}
// Closes the currently open modal (if any). Useful when a modal was
// opened outside of a blocking CUTSCENE_MODAL item (e.g. directly via
// uiModalOpen) and this cutscene just needs to dismiss it and continue
// on to whatever follows this item in the sequence.
#define CUTSCENE_MODAL_CLOSE() \
{ .type = CUTSCENE_ITEM_TYPE_MODAL_CLOSE }
// Opens the on-screen keyboard and blocks the cutscene until it closes,
// then caches whatever was typed - see cutsceneSystemGetTextCache to
// read it back afterwards. __VA_ARGS__ are uikeyboardopen_t designated
// initializers, e.g. CUTSCENE_KEYBOARD(.cancel = true, .maxLength = 8).
#define CUTSCENE_KEYBOARD(...) \
{ \
.type = CUTSCENE_ITEM_TYPE_KEYBOARD, \
.keyboard = { .open = { __VA_ARGS__ } } \
}
// (Re)requests an available save device and jumps straight to
// SUCCESS_MARKER or FAILURE_MARKER once it resolves, same as a
// CUTSCENE_MODAL_OPTIONS callback - it does not fall through to
// whatever follows this item, so both markers must be scripted
// elsewhere in the same cutscene.
#define CUTSCENE_SAVE_DEVICE_CHECK(SUCCESS_MARKER, FAILURE_MARKER) \
{ \
.type = CUTSCENE_ITEM_TYPE_SAVE_DEVICE_CHECK, \
.saveDeviceCheck = { \
.successMarker = SUCCESS_MARKER, \
.failureMarker = FAILURE_MARKER \
} \
}
// (Re)loads every save slot via saveLoadAllSlots() and jumps straight to
// SUCCESS_MARKER or FAILURE_MARKER once it resolves, same shape as
// CUTSCENE_SAVE_DEVICE_CHECK - it does not fall through to whatever
// follows this item, so both markers must be scripted elsewhere in the
// same cutscene.
#define CUTSCENE_SAVE_LOAD_ALL_SLOTS(SUCCESS_MARKER, FAILURE_MARKER) \
{ \
.type = CUTSCENE_ITEM_TYPE_SAVE_LOAD_ALL_SLOTS, \
.saveLoadAllSlots = { \
.successMarker = SUCCESS_MARKER, \
.failureMarker = FAILURE_MARKER \
} \
}
#define CUTSCENE_ENTITY_WALK_TO(ENTITY_INDEX, X, Y, Z) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO, \
.entityWalkTo = { \
.entityIndex = ENTITY_INDEX, \
.positions = (const worldpos_t[]){ { X, Y, Z } }, \
.count = 1, \
.walkAround = true \
} \
}
#define CUTSCENE_ENTITY_WALK_PATH(NAME, ENTITY_INDEX, ...) \
static const worldpos_t CUTSCENE_##NAME##_POSITIONS[] = { __VA_ARGS__ }; \
static const cutsceneitem_t CUTSCENE_##NAME = { \
.type = CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO, \
.entityWalkTo = { \
.entityIndex = ENTITY_INDEX, \
.positions = CUTSCENE_##NAME##_POSITIONS, \
.count = sizeof(CUTSCENE_##NAME##_POSITIONS) / sizeof(worldpos_t), \
.walkAround = true \
} \
}
#define CUTSCENE_ENTITY_REMOVE(ENTITY_INDEX) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_REMOVE, \
.entityRemove = { .entityIndex = ENTITY_INDEX } \
}
#define CUTSCENE_ENTITY_ADD(TYPE, X, Y, Z) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_ADD, \
.entityAdd = { .entityType = TYPE, .position = { X, Y, Z } } \
}
#define CUTSCENE_ENTITY_TURN(ENTITY_INDEX, DIRECTION) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_TURN, \
.entityTurn = { .entityIndex = ENTITY_INDEX, .direction = DIRECTION } \
}
// Walks ENTITY_INDEX to stand beside TARGET_ENTITY_INDEX, offset by
// (OFFSET_X, OFFSET_Y) on the 2D plane. The destination Z is resolved
// from nearby terrain each frame, so ramps between the two entities are
// accounted for automatically.
#define CUTSCENE_ENTITY_WALK_TO_ENTITY( \
ENTITY_INDEX, TARGET_ENTITY_INDEX, OFFSET_X, OFFSET_Y \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO_ENTITY, \
.entityWalkToEntity = { \
.entityIndex = ENTITY_INDEX, \
.targetEntityIndex = TARGET_ENTITY_INDEX, \
.offsetX = OFFSET_X, \
.offsetY = OFFSET_Y \
} \
}
#define CUTSCENE_ENTITY_TELEPORT(ENTITY_INDEX, X, Y, Z) \
{ \
.type = CUTSCENE_ITEM_TYPE_ENTITY_TELEPORT, \
.entityTeleport = { .entityIndex = ENTITY_INDEX, .target = { X, Y, Z } } \
}
#define CUTSCENE_FADE(FROM, TO, DURATION, EASING) \
{ \
.type = CUTSCENE_ITEM_TYPE_FADE, \
.fade = { .from = FROM, .to = TO, .duration = DURATION, .easing = EASING } \
}
#define CUTSCENE_FADE_TO_BLACK(DURATION) \
CUTSCENE_FADE(COLOR_TRANSPARENT_BLACK, COLOR_BLACK, DURATION, EASING_LINEAR)
#define CUTSCENE_FADE_FROM_BLACK(DURATION) \
CUTSCENE_FADE(COLOR_BLACK, COLOR_TRANSPARENT_BLACK, DURATION, EASING_LINEAR)
#define CUTSCENE_FADE_TO_WHITE(DURATION) \
CUTSCENE_FADE(COLOR_TRANSPARENT_WHITE, COLOR_WHITE, DURATION, EASING_LINEAR)
#define CUTSCENE_FADE_FROM_WHITE(DURATION) \
CUTSCENE_FADE(COLOR_WHITE, COLOR_TRANSPARENT_WHITE, DURATION, EASING_LINEAR)
#define CUTSCENE_EMOJI(ENTITY_INDEX, EMOJI_TYPE, DURATION) \
{ \
.type = CUTSCENE_ITEM_TYPE_EMOJI, \
.emoji = { \
.entityIndex = ENTITY_INDEX, \
.emojiType = EMOJI_TYPE, \
.duration = DURATION \
} \
}
// AMOUNT ranges 0 (no shake) to 4 (three tiles): 1 is half a tile, 2 is
// a full tile, 3 is two tiles, and 4 is three tiles.
#define CUTSCENE_SHAKE(AMOUNT, DURATION) \
{ \
.type = CUTSCENE_ITEM_TYPE_SHAKE, \
.shake = { .amount = AMOUNT, .duration = DURATION } \
}
// Waits until BATTLE.state reaches STATE. Put this BEFORE
// CUTSCENE_SET_PAUSE(CUTSCENE_PAUSE_BATTLE), not after -- pausing first
// freezes BATTLE.state wherever it already is, so it would never reach
// STATE on its own to satisfy the wait. Waiting unpaused, then pausing the
// moment it's satisfied, catches the battle right at STATE before it can
// advance further.
#define CUTSCENE_BATTLE_WAIT_STATE(STATE) \
{ \
.type = CUTSCENE_ITEM_TYPE_BATTLE_WAIT_STATE, \
.battleWaitState = { .state = STATE } \
}
// Immediately queues an attack for FIGHTER_INDEX against TARGET_INDEX,
// bypassing normal player/AI selection for that fighter this round.
#define CUTSCENE_BATTLE_FORCE_ACTION(FIGHTER_INDEX, TARGET_INDEX) \
{ \
.type = CUTSCENE_ITEM_TYPE_BATTLE_FORCE_ACTION, \
.battleForceAction = { \
.fighterIndex = FIGHTER_INDEX, .targetIndex = TARGET_INDEX \
} \
}
#define CUTSCENE_SET_PAUSE(FLAGS) \
{ .type = CUTSCENE_ITEM_TYPE_SET_PAUSE, .setPause = (FLAGS) }
#define CUTSCENE_ITEM_GIVE(ITEM_ID, QUANTITY) \
{ \
.type = CUTSCENE_ITEM_TYPE_ITEM_GIVE, \
.itemGive = { .item = ITEM_ID, .quantity = QUANTITY } \
}
// Runs all listed items simultaneously and waits until all are done.
// Concurrent items cannot be nested inside another CUTSCENE_CONCURRENT.
#define CUTSCENE_CONCURRENT(...) \
{ \
.type = CUTSCENE_ITEM_TYPE_CONCURRENT, \
.concurrent = { \
.items = (const cutsceneitem_t[]){ __VA_ARGS__ }, \
.count = (uint8_t)( \
sizeof((cutsceneitem_t[]){ __VA_ARGS__ }) / \
sizeof(cutsceneitem_t) \
) \
} \
}
#define CUTSCENE_MAP_AREA_ADD( \
MIN_X, MIN_Y, MIN_Z, MAX_X, MAX_Y, MAX_Z, CALLBACK, NOTIFY, TRIGGER \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_MAP_AREA_ADD, \
.mapAreaAdd = { \
.min = { MIN_X, MIN_Y, MIN_Z }, \
.max = { MAX_X, MAX_Y, MAX_Z }, \
.callback = CALLBACK, \
.notify = NOTIFY, \
.trigger = TRIGGER \
} \
}
#define CUTSCENE_MAP_AREA_REMOVE(AREA_ID) \
{ \
.type = CUTSCENE_ITEM_TYPE_MAP_AREA_REMOVE, \
.mapAreaRemove = { .areaId = AREA_ID } \
}
// Waits until any one of the given map area IDs has its callback invoked.
// Accepts CUTSCENE_AREA_LAST_CREATED in place of a literal area ID.
#define CUTSCENE_MAP_AREA_WAIT(...) \
{ \
.type = CUTSCENE_ITEM_TYPE_MAP_AREA_WAIT, \
.mapAreaWait = { \
.areaIds = (const uint8_t[]){ __VA_ARGS__ }, \
.count = (uint8_t)( \
sizeof((const uint8_t[]){ __VA_ARGS__ }) / sizeof(uint8_t) \
) \
} \
}
// Adds a map area, waits for it to be triggered once, then removes it
// before the cutscene continues. Uses a no-op callback since the wait is
// driven by the area's trigger count rather than callback logic.
#define CUTSCENE_MAP_AREA_TRIGGER_ONCE( \
MIN_X, MIN_Y, MIN_Z, MAX_X, MAX_Y, MAX_Z, NOTIFY, TRIGGER \
) \
CUTSCENE_MAP_AREA_ADD( \
MIN_X, MIN_Y, MIN_Z, MAX_X, MAX_Y, MAX_Z, \
mapAreaNoopCallback, NOTIFY, TRIGGER \
), \
CUTSCENE_MAP_AREA_WAIT(CUTSCENE_AREA_LAST_CREATED), \
CUTSCENE_MAP_AREA_REMOVE(CUTSCENE_AREA_LAST_CREATED)
// Longhand - every audioMixerQueuePlay() parameter. See
// CUTSCENE_AUDIO_PLAY_SIMPLE/CUTSCENE_AUDIO_PLAY_LOOPED below for the
// shorthands.
#define CUTSCENE_AUDIO_PLAY( \
FILE, CHANNEL, VOLUME, PAN, LOOPING, LOOP_COUNT, LOOP_START, LOOP_TO \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_PLAY, \
.audioPlay = { \
.file = FILE, \
.channel = CHANNEL, \
.volume = VOLUME, \
.pan = PAN, \
.looping = LOOPING, \
.loopCount = LOOP_COUNT, \
.loopStart = LOOP_START, \
.loopTo = LOOP_TO \
} \
}
// Shorthand: play FILE on CHANNEL once, full volume, centered (0.0f, same
// as AUDIO_STREAM_CENTER), no loop.
#define CUTSCENE_AUDIO_PLAY_SIMPLE(FILE, CHANNEL) \
CUTSCENE_AUDIO_PLAY(FILE, CHANNEL, 1.0f, 0.0f, false, 0, -1.0f, 0.0f)
// Shorthand: play FILE on CHANNEL looping the whole clip forever, full
// volume, centered (0.0f, same as AUDIO_STREAM_CENTER).
#define CUTSCENE_AUDIO_PLAY_LOOPED(FILE, CHANNEL) \
CUTSCENE_AUDIO_PLAY(FILE, CHANNEL, 1.0f, 0.0f, true, 0, -1.0f, 0.0f)
#define CUTSCENE_AUDIO_STOP(CHANNEL) \
{ .type = CUTSCENE_ITEM_TYPE_AUDIO_STOP, .audioStopChannel = CHANNEL }
#define CUTSCENE_AUDIO_PAUSE(CHANNEL) \
{ .type = CUTSCENE_ITEM_TYPE_AUDIO_PAUSE, .audioPauseChannel = CHANNEL }
#define CUTSCENE_AUDIO_RESUME(CHANNEL) \
{ .type = CUTSCENE_ITEM_TYPE_AUDIO_RESUME, .audioResumeChannel = CHANNEL }
// Longhand - starts CHANNEL fading from FROM to TO over DURATION seconds,
// eased by EASING. Doesn't wait for it - pair with
// CUTSCENE_AUDIO_FADE_WAIT(CHANNEL) to block until it finishes. See
// CUTSCENE_AUDIO_FADE_OUT/CUTSCENE_AUDIO_FADE_IN below for the shorthands;
// use this directly for a custom range, e.g.
// CUTSCENE_AUDIO_FADE(CHANNEL, 1.0f, 0.3f, 5.0f, EASING_LINEAR) to fade
// down to "playing quietly" rather than silent.
#define CUTSCENE_AUDIO_FADE(CHANNEL, FROM, TO, DURATION, EASING) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_FADE, \
.audioFade = { \
.channel = CHANNEL, \
.from = FROM, \
.to = TO, \
.duration = DURATION, \
.easing = EASING \
} \
}
// Shorthand: fade CHANNEL from full volume to silent over DURATION seconds.
#define CUTSCENE_AUDIO_FADE_OUT(CHANNEL, DURATION) \
CUTSCENE_AUDIO_FADE(CHANNEL, 1.0f, 0.0f, DURATION, EASING_LINEAR)
// Shorthand: fade CHANNEL from silent to full volume over DURATION seconds.
#define CUTSCENE_AUDIO_FADE_IN(CHANNEL, DURATION) \
CUTSCENE_AUDIO_FADE(CHANNEL, 0.0f, 1.0f, DURATION, EASING_LINEAR)
// Waits until CHANNEL's fade (started elsewhere by CUTSCENE_AUDIO_FADE)
// has finished - a no-op wait if CHANNEL isn't fading.
#define CUTSCENE_AUDIO_FADE_WAIT(CHANNEL) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_FADE_WAIT, \
.audioFadeWaitChannel = CHANNEL \
}
#define CUTSCENE_AUDIO_SET_PAN(CHANNEL, PAN) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_SET_PAN, \
.audioSetPan = { .channel = CHANNEL, .pan = PAN } \
}
#define CUTSCENE_AUDIO_SET_LOOP( \
CHANNEL, LOOPING, LOOP_COUNT, LOOP_START, LOOP_TO \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_SET_LOOP, \
.audioSetLoop = { \
.channel = CHANNEL, \
.looping = LOOPING, \
.loopCount = LOOP_COUNT, \
.loopStart = LOOP_START, \
.loopTo = LOOP_TO \
} \
}
// Combined form of CUTSCENE_AUDIO_SET_PAN + CUTSCENE_AUDIO_SET_LOOP, for
// setting both at once rather than as two separate items.
#define CUTSCENE_AUDIO_SET( \
CHANNEL, PAN, LOOPING, LOOP_COUNT, LOOP_START, LOOP_TO \
) \
{ \
.type = CUTSCENE_ITEM_TYPE_AUDIO_SET, \
.audioSet = { \
.channel = CHANNEL, \
.pan = PAN, \
.looping = LOOPING, \
.loopCount = LOOP_COUNT, \
.loopStart = LOOP_START, \
.loopTo = LOOP_TO \
} \
}
/**
* Same as cutsceneAppend, but inserts items starting at atIndex instead of
* at the end - e.g. pass CUTSCENE_SYSTEM.currentItem + 1 to make these
* items run immediately after the currently-executing one. Every existing
* item from atIndex onward shifts right by count to make room.
*
* @param scene Cutscene to insert into.
* @param itemsMax Capacity of scene->items.
* @param atIndex Index the first inserted item should end up at - must be
* <= scene->itemCount.
* @param items Items to insert, copied in order.
* @param count Number of entries in items.
*/
void cutsceneAppendNext(
cutscene_t *scene,
const size_t itemsMax,
const uint8_t atIndex,
const cutsceneitem_t *items,
const uint8_t count
);
+99 -7
View File
@@ -11,10 +11,37 @@
#include "util/string.h"
#include "assert/assert.h"
#include "asset/asset.h"
#include "asset/loader/assetloader.h"
#include "ui/overlay/uifatalerror.h"
cutscenesystem_t CUTSCENE_SYSTEM;
cutsceneitem_t CUTSCENE_LOADED_ITEMS[CUTSCENE_LOADED_ITEMS_MAX];
cutscene_t CUTSCENE_LOADED_SCENE = {
.items = CUTSCENE_LOADED_ITEMS,
.itemCount = 0,
.pause = CUTSCENE_PAUSE_NONE
};
static yyjson_doc *CUTSCENE_LOADED_DOC = NULL;
void cutsceneLoadedSetDoc(yyjson_doc *doc) {
if(CUTSCENE_LOADED_DOC != NULL) yyjson_doc_free(CUTSCENE_LOADED_DOC);
CUTSCENE_LOADED_DOC = doc;
}
cutsceneitem_t CUTSCENE_INSERT_ITEMS[CUTSCENE_INSERT_ITEMS_MAX];
cutscene_t CUTSCENE_INSERT_SCENE = {
.items = CUTSCENE_INSERT_ITEMS,
.itemCount = 0,
.pause = CUTSCENE_PAUSE_NONE
};
static yyjson_doc *CUTSCENE_INSERT_DOC = NULL;
void cutsceneInsertSetDoc(yyjson_doc *doc) {
if(CUTSCENE_INSERT_DOC != NULL) yyjson_doc_free(CUTSCENE_INSERT_DOC);
CUTSCENE_INSERT_DOC = doc;
}
void cutsceneSystemInit() {
memoryZero(&CUTSCENE_SYSTEM, sizeof(cutscenesystem_t));
}
@@ -24,11 +51,6 @@ void cutsceneSystemPrepare(
entity_t *interact,
entity_t *interacted
) {
assertTrue(
cutscene->dataSize < CUTSCENE_SYSTEM_SIZE_MAX,
"Cutscene data size exceeds CUTSCENE_SYSTEM_SIZE_MAX"
);
CUTSCENE_SYSTEM.scene = cutscene;
CUTSCENE_SYSTEM.pause = cutscene->pause;
CUTSCENE_SYSTEM.entityInteract = interact;
@@ -40,6 +62,7 @@ void cutsceneSystemPrepare(
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.currentItem = 0xFF;// Set to 0xFF so Next wraps to 0.
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
}
void cutsceneSystemStartCutscene(const cutscene_t *cutscene) {
@@ -67,15 +90,43 @@ void cutsceneSystemLoad(const char_t *file) {
char_t path[ASSET_FILE_NAME_MAX];
stringFormat(path, sizeof(path), "cutscenes/%s", file);
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_CUTSCENE, NULL);
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_JSON, NULL);
errorret_t result = assetRequireLoaded(entry);
if(errorIsNotOk(result)) {
assetUnlockEntry(entry);
errorCatch(errorPrint(result));
uiFatalErrorOpen(NULL);
return;
}
cutsceneSystemStartCutscene(&entry->data.cutscene.cutscene);
// Steal the parsed doc before unlocking - assetJsonDispose would
// otherwise free it out from under CUTSCENE_LOADED_ITEMS' name pointers
// the moment this entry gets reaped.
yyjson_doc *doc = entry->data.json;
entry->data.json = NULL;
assetUnlockEntry(entry);
// Force this now-zero-ref entry to actually go away right away, rather
// than leaving it languishing as LOADED until something else happens to
// trigger a reap - assetRequireLoaded treats an already-LOADED entry as
// an instant cache hit with no reparsing, so a stale leftover entry for
// this same path would otherwise make a later load of it wrongly skip
// reloading, still returning CUTSCENE_LOADED_SCENE even after some
// other cutscene has since overwritten it.
assetReapUnused();
errorret_t parseResult = cutsceneParseDoc(
doc, &CUTSCENE_LOADED_SCENE, CUTSCENE_LOADED_ITEMS,
CUTSCENE_LOADED_ITEMS_MAX
);
if(errorIsNotOk(parseResult)) {
yyjson_doc_free(doc);
errorCatch(errorPrint(parseResult));
uiFatalErrorOpen(NULL);
return;
}
cutsceneLoadedSetDoc(doc);
cutsceneSystemStartCutscene(&CUTSCENE_LOADED_SCENE);
}
void cutsceneRestart(void) {
@@ -107,6 +158,21 @@ void cutsceneSystemNext() {
CUTSCENE_SYSTEM.currentItem++;
// Run off the end of the current scene? If it was spliced in via
// CUTSCENE_ITEM_TYPE_INSERT, pop back to whatever it was inserted into
// and resume right after the INSERT item - possibly chaining through
// several pops if that lands on the end of its own parent in turn (an
// insert as the very last item, or a nested insert-of-an-insert).
while(
CUTSCENE_SYSTEM.currentItem >= CUTSCENE_SYSTEM.scene->itemCount &&
CUTSCENE_SYSTEM.insertStackSize > 0
) {
cutsceneinsertframe_t frame =
CUTSCENE_SYSTEM.insertStack[--CUTSCENE_SYSTEM.insertStackSize];
CUTSCENE_SYSTEM.scene = frame.scene;
CUTSCENE_SYSTEM.currentItem = frame.currentItem + 1;
}
// End of the cutscene?
if(
CUTSCENE_SYSTEM.currentItem >= CUTSCENE_SYSTEM.scene->itemCount
@@ -129,6 +195,7 @@ void cutsceneSystemNext() {
CUTSCENE_SYSTEM.textMiniLastCreated = CUTSCENE_TEXT_MINI_LAST_CREATED;
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
if(onComplete != NULL) onComplete(userData);
return;
@@ -140,6 +207,27 @@ void cutsceneSystemNext() {
cutsceneItemStart(item, &CUTSCENE_SYSTEM.data);
}
void cutsceneSystemInsertCutscene(const cutscene_t *cutscene) {
assertNotNull(
CUTSCENE_SYSTEM.scene,
"cutsceneSystemInsertCutscene called with no cutscene running"
);
assertTrue(
CUTSCENE_SYSTEM.insertStackSize < CUTSCENE_INSERT_STACK_MAX,
"Cutscene INSERT nesting exceeds CUTSCENE_INSERT_STACK_MAX"
);
CUTSCENE_SYSTEM.insertStack[CUTSCENE_SYSTEM.insertStackSize++] =
(cutsceneinsertframe_t){
.scene = CUTSCENE_SYSTEM.scene,
.currentItem = CUTSCENE_SYSTEM.currentItem
};
CUTSCENE_SYSTEM.scene = cutscene;
CUTSCENE_SYSTEM.currentItem = 0xFF;// Set to 0xFF so Next wraps to 0.
cutsceneSystemNext();
}
void cutsceneSystemSetOnComplete(cutscenecallback_t onComplete) {
assertNotNull(
CUTSCENE_SYSTEM.scene,
@@ -259,4 +347,8 @@ void cutsceneSystemDispose() {
CUTSCENE_SYSTEM.textMiniLastCreated = CUTSCENE_TEXT_MINI_LAST_CREATED;
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
cutsceneLoadedSetDoc(NULL);
cutsceneInsertSetDoc(NULL);
}
+118 -13
View File
@@ -17,8 +17,8 @@ typedef struct entity_s entity_t;
#define CUTSCENE_AREA_LAST_CREATED ((uint8_t)0xFF)
#define CUTSCENE_TEXT_MINI_LAST_CREATED ((uint8_t)0xFA)
// Maximum number of bytes a running cutscene may request via
// cutscene_t.dataSize.
// Size in bytes of cutscenesystem_t.userData, the running cutscene's
// scratch space.
#define CUTSCENE_SYSTEM_SIZE_MAX 8192
// Size of CUTSCENE_SYSTEM.textCache - keep >= UI_KEYBOARD_TEXT_MAX (see
@@ -26,6 +26,82 @@ typedef struct entity_s entity_t;
// CUTSCENE_ITEM_TYPE_KEYBOARD item is never truncated caching it here.
#define CUTSCENE_TEXT_CACHE_MAX 64
// Only one JSON-authored cutscene is ever loaded/playing at a time - a
// CUTSCENE item hands control off completely (see cutsceneCutsceneStart)
// and cutsceneSystemLoad/cutsceneCutsceneResolve release their asset lock
// the instant loading finishes - so there's no benefit to malloc'ing a
// fresh items buffer per load. Instead those functions parse directly
// into this fixed, static buffer via cutsceneParseDoc (see cutscene.h),
// once the raw JSON itself has been read+parsed via the generic
// ASSET_LOADER_TYPE_JSON asset loader. Bump this if a real cutscene ever
// needs more; C-authored cutscenes (used only by tests) don't use this
// at all - they're compiled directly into their own static const arrays.
#define CUTSCENE_LOADED_ITEMS_MAX 128
extern cutsceneitem_t CUTSCENE_LOADED_ITEMS[CUTSCENE_LOADED_ITEMS_MAX];
// The cutscene_t for whatever was last parsed into CUTSCENE_LOADED_ITEMS -
// .items always points at CUTSCENE_LOADED_ITEMS; itemCount/pause are
// overwritten by the loader on each load.
extern cutscene_t CUTSCENE_LOADED_SCENE;
/**
* Frees whatever yyjson_doc currently backs CUTSCENE_LOADED_ITEMS' string
* fields (marker/cutscene-reference names point directly into it - see
* cutsceneCutsceneRefLoad/cutsceneMarkerLoad) and takes ownership of doc
* instead. Called by cutsceneSystemLoad/cutsceneCutsceneResolve once a
* fresh parse into CUTSCENE_LOADED_ITEMS completes successfully, and from
* cutsceneSystemDispose to release the last one at shutdown.
*
* @param doc The new doc to take ownership of (NULL just frees/clears the
* current one).
*/
void cutsceneLoadedSetDoc(yyjson_doc *doc);
// A second, much smaller buffer dedicated to CUTSCENE_ITEM_TYPE_INSERT
// (see cutsceneSystemInsertCutscene) - kept entirely separate from
// CUTSCENE_LOADED_ITEMS so that a JSON-authored INSERT item resolving its
// target by name (via cutsceneInsertResolve) never overwrites the buffer
// the currently-running outer cutscene's own items live in. Sized small
// deliberately - an inserted snippet is meant to be a short splice, not a
// full cutscene.
#define CUTSCENE_INSERT_ITEMS_MAX 16
extern cutsceneitem_t CUTSCENE_INSERT_ITEMS[CUTSCENE_INSERT_ITEMS_MAX];
// The cutscene_t for whatever was last parsed into CUTSCENE_INSERT_ITEMS -
// .items always points at CUTSCENE_INSERT_ITEMS. Only one JSON-authored
// INSERT target is ever resident at a time, same restriction as
// CUTSCENE_LOADED_SCENE - a JSON-authored INSERT whose target itself
// contains a JSON-authored INSERT would overwrite this out from under
// itself, so that isn't supported (compile-time CUTSCENE_INSERT nesting is
// unaffected, since it never touches asset loading).
extern cutscene_t CUTSCENE_INSERT_SCENE;
/**
* Same as cutsceneLoadedSetDoc, but for whatever yyjson_doc currently
* backs CUTSCENE_INSERT_ITEMS' string fields. Called by
* cutsceneInsertResolve once a fresh parse into CUTSCENE_INSERT_ITEMS
* completes successfully, and from cutsceneSystemDispose to release the
* last one at shutdown.
*
* @param doc The new doc to take ownership of (NULL just frees/clears the
* current one).
*/
void cutsceneInsertSetDoc(yyjson_doc *doc);
// Maximum nesting depth of CUTSCENE_ITEM_TYPE_INSERT items - how many
// "return to the item after this one" frames cutsceneSystemInsertCutscene
// may have pushed at once (see CUTSCENE_SYSTEM.insertStack).
#define CUTSCENE_INSERT_STACK_MAX 4
// One "return address" pushed by cutsceneSystemInsertCutscene - the scene
// and item index to resume at once the inserted cutscene's items run out.
typedef struct {
const cutscene_t *scene;
uint8_t currentItem;
} cutsceneinsertframe_t;
typedef struct {
const cutscene_t *scene;
uint8_t currentItem;
@@ -46,12 +122,17 @@ typedef struct {
// Data (used by the current item).
cutsceneitemdata_t data;
// Custom user data for the running cutscene, sized per-scene by
// cutscene_t.dataSize.
// Custom user data for the running cutscene.
uint8_t userData[CUTSCENE_SYSTEM_SIZE_MAX];
// See cutsceneSystemSetOnComplete.
cutscenecallback_t onComplete;
// Return stack pushed by CUTSCENE_ITEM_TYPE_INSERT items - see
// cutsceneSystemInsertCutscene. Reset (insertStackSize = 0) whenever a
// cutscene freshly starts or the system is disposed.
cutsceneinsertframe_t insertStack[CUTSCENE_INSERT_STACK_MAX];
uint8_t insertStackSize;
} cutscenesystem_t;
extern cutscenesystem_t CUTSCENE_SYSTEM;
@@ -97,15 +178,39 @@ void cutsceneSystemStartCutsceneAndGoToMarker(
);
/**
* Locks, loads and immediately starts a cutscene asset by file name,
* e.g. cutsceneSystemLoad("main_menu.jsonc") loads and starts
* assets/cutscenes/main_menu.jsonc - same lock/assetRequireLoaded/start
* sequence as sceneInitialInit's INITIAL_CUTSCENE_ENTRY. The entry is
* locked forever (never unlocked), same lifetime convention as
* LOCALE.entry - it's fine to call this again later for the same file,
* it'll just resolve to the same already-loaded entry. Opens the fatal
* error overlay (see uiFatalErrorOpen) instead of starting anything if
* the asset fails to load.
* Splices cutscene's items into the running cutscene in place, resuming
* the current cutscene right after the CUTSCENE_ITEM_TYPE_INSERT item that
* called this once cutscene's items run out - unlike
* cutsceneSystemStartCutscene (a one-way jump that replaces the running
* cutscene outright). Pushes a return frame onto
* CUTSCENE_SYSTEM.insertStack; cutsceneSystemNext pops it once cutscene
* ends. Deliberately does not touch pause flags, interact entities,
* "last created" state, the text cache or userData - those all keep
* whatever the outer cutscene set, since this is meant to feel like
* pasting cutscene's items in place rather than starting an independent
* cutscene. Because of this, cutscene's own .pause is ignored.
* Asserts if no cutscene is currently running, or if inserts are already
* nested CUTSCENE_INSERT_STACK_MAX deep.
*
* Note: cutsceneRestart() resets the insert stack (via
* cutsceneSystemPrepare), so a RESTART item used from inside an inserted
* cutscene restarts just that inserted cutscene and does not return to
* whatever it was inserted into.
*
* @param cutscene The cutscene whose items to splice in.
*/
void cutsceneSystemInsertCutscene(const cutscene_t *cutscene);
/**
* Loads and immediately starts a cutscene asset by file name, e.g.
* cutsceneSystemLoad("main_menu.jsonc") loads and starts
* assets/cutscenes/main_menu.jsonc, parsing it into
* CUTSCENE_LOADED_SCENE/ITEMS - the underlying JSON asset entry is
* unlocked and reaped immediately once that's done (see
* CUTSCENE_LOADED_ITEMS_MAX's doc comment for why), so it's always
* re-read+re-parsed fresh, never assumed still resident from a previous
* call. Opens the fatal error overlay (see uiFatalErrorOpen) instead of
* starting anything if the asset fails to load.
*
* @param file Cutscene file name (with .jsonc extension), relative to
* assets/cutscenes/.
@@ -33,9 +33,7 @@ bool_t cutsceneAudioFadeUpdate(
errorret_t cutsceneAudioFadeLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
audiomixerchannel_t channelValue;
if(!cutsceneJsonLookupAudioChannel(
@@ -48,13 +48,9 @@ bool_t cutsceneAudioFadeUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_FADE needs no pool storage).
* @param poolOffset Unused (AUDIO_FADE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioFadeLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -18,9 +18,7 @@ bool_t cutsceneAudioFadeWaitUpdate(
errorret_t cutsceneAudioFadeWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
return cutsceneJsonLoadAudioChannelOnly(itemObj, &item->audioFadeWaitChannel);
}
@@ -28,13 +28,9 @@ bool_t cutsceneAudioFadeWaitUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_FADE_WAIT needs no pool storage).
* @param poolOffset Unused (AUDIO_FADE_WAIT needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioFadeWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneAudioPauseUpdate(
errorret_t cutsceneAudioPauseLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
return cutsceneJsonLoadAudioChannelOnly(itemObj, &item->audioPauseChannel);
}
@@ -37,13 +37,9 @@ bool_t cutsceneAudioPauseUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_PAUSE needs no pool storage).
* @param poolOffset Unused (AUDIO_PAUSE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioPauseLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -34,9 +34,7 @@ bool_t cutsceneAudioPlayUpdate(
errorret_t cutsceneAudioPlayLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonCopyString(
itemObj, "file", NULL, item->audioPlay.file, AUDIO_PATH_MAX
@@ -50,13 +50,9 @@ bool_t cutsceneAudioPlayUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_PLAY needs no pool storage).
* @param poolOffset Unused (AUDIO_PLAY needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioPlayLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneAudioResumeUpdate(
errorret_t cutsceneAudioResumeLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
return cutsceneJsonLoadAudioChannelOnly(itemObj, &item->audioResumeChannel);
}
@@ -37,13 +37,9 @@ bool_t cutsceneAudioResumeUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_RESUME needs no pool storage).
* @param poolOffset Unused (AUDIO_RESUME needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioResumeLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -33,9 +33,7 @@ bool_t cutsceneAudioSetUpdate(
errorret_t cutsceneAudioSetLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
audiomixerchannel_t channelValue;
if(!cutsceneJsonLookupAudioChannel(
@@ -49,13 +49,9 @@ bool_t cutsceneAudioSetUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_SET needs no pool storage).
* @param poolOffset Unused (AUDIO_SET needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioSetLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -32,9 +32,7 @@ bool_t cutsceneAudioSetLoopUpdate(
errorret_t cutsceneAudioSetLoopLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
audiomixerchannel_t channelValue;
if(!cutsceneJsonLookupAudioChannel(
@@ -46,13 +46,9 @@ bool_t cutsceneAudioSetLoopUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_SET_LOOP needs no pool storage).
* @param poolOffset Unused (AUDIO_SET_LOOP needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioSetLoopLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -26,9 +26,7 @@ bool_t cutsceneAudioSetPanUpdate(
errorret_t cutsceneAudioSetPanLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
audiomixerchannel_t channelValue;
if(!cutsceneJsonLookupAudioChannel(
@@ -43,13 +43,9 @@ bool_t cutsceneAudioSetPanUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_SET_PAN needs no pool storage).
* @param poolOffset Unused (AUDIO_SET_PAN needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioSetPanLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneAudioStopUpdate(
errorret_t cutsceneAudioStopLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
return cutsceneJsonLoadAudioChannelOnly(itemObj, &item->audioStopChannel);
}
@@ -37,13 +37,9 @@ bool_t cutsceneAudioStopUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (AUDIO_STOP needs no pool storage).
* @param poolOffset Unused (AUDIO_STOP needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneAudioStopLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -8,4 +8,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
cutscenestartbattle.c
cutscenebattlewaitstate.c
cutscenebattleforceaction.c
cutsceneregularbattle.c
)
@@ -26,9 +26,7 @@ bool_t cutsceneBattleForceActionUpdate(
errorret_t cutsceneBattleForceActionLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
item->battleForceAction.fighterIndex =
(uint8_t)yyjson_get_int(yyjson_obj_get(itemObj, "fighterIndex"));
@@ -44,13 +44,9 @@ bool_t cutsceneBattleForceActionUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (BATTLE_FORCE_ACTION needs no pool storage).
* @param poolOffset Unused (BATTLE_FORCE_ACTION needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneBattleForceActionLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -17,9 +17,7 @@ bool_t cutsceneBattleWaitStateUpdate(
errorret_t cutsceneBattleWaitStateLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
battlestate_t stateValue;
if(!cutsceneJsonLookupBattleState(
@@ -32,13 +32,9 @@ bool_t cutsceneBattleWaitStateUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (BATTLE_WAIT_STATE needs no pool storage).
* @param poolOffset Unused (BATTLE_WAIT_STATE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneBattleWaitStateLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpg/cutscene/item/cutsceneitem.h"
bool_t cutsceneRegularBattleUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
return true;
}
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/cutscene/item/cutsceneitembase.h"
/**
* Updates a regular-battle step. By the time this item runs, the preceding
* START_BATTLE item has already waited out the whole encounter, synced
* party HP/MP, and returned to the overworld scene - so this is currently
* just a completes-immediately hook point for whatever regular-battle-
* specific follow-up (loot, post-battle dialogue, etc.) gets added later.
*
* @param item The cutscene item.
* @param data Runtime data storage.
* @returns true always.
*/
bool_t cutsceneRegularBattleUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
);
// REGULAR_BATTLE has no fields of its own to parse yet - see
// cutsceneItemLoadNoop (cutsceneitembase.h), used directly as its .load
// callback.
@@ -8,8 +8,11 @@
#include "rpg/cutscene/item/cutsceneitem.h"
#include "rpg/battle/party.h"
#include "scene/scene.h"
#include "save/save.h"
#include "rpg/cutscene/item/json/cutscenejsonbattleencounter.h"
#include "rpg/cutscene/item/json/cutscenejsonbattleability.h"
#include "rpg/cutscene/item/json/cutscenejsonfield.h"
#include "util/memory.h"
void cutsceneStartBattleStart(
const cutsceneitem_t *item,
@@ -19,8 +22,9 @@ void cutsceneStartBattleStart(
battleInit();
for(uint8_t i = 0; i < PARTY_ACTIVE_SIZE_MAX; i++) {
battlefighter_t *member = partyGetOrderMember(i);
for(uint8_t i = 0; i < config->playerCount; i++) {
battlefighter_t *member =
partyGetOrderMember(config->players[i]);
if(member == NULL) continue;
battlefighter_t *fighter = battleAddFighter(
@@ -32,14 +36,18 @@ void cutsceneStartBattleStart(
fighter->health = member->health;
fighter->mp = member->mp;
fighter->status = member->status;
memoryCopy(fighter->abilities, member->abilities, sizeof(fighter->abilities));
}
for(uint8_t i = 0; i < config->enemyCount; i++) {
const cutscenestartbattleenemy_t *enemy = &config->enemies[i];
battleAddFighter(
battlefighter_t *fighter = battleAddFighter(
BATTLE_FIGHTER_TEAM_ENEMY, BATTLE_FIGHTER_CONTROLLER_AI,
enemy->stats, enemy->healthMax, enemy->mpMax
);
if(fighter == NULL) continue;
memoryCopy(fighter->abilities, enemy->abilities, sizeof(fighter->abilities));
}
battleStart(config->encounterType, config->fleeAvailable);
@@ -52,13 +60,15 @@ bool_t cutsceneStartBattleUpdate(
) {
if(BATTLE.result == BATTLE_RESULT_NONE) return false;
const cutscenestartbattle_t *config = &item->startBattle;
// Sync ally HP/MP back to the persistent party roster. Relies on
// ally fighters having been added to BATTLE.fighters in the same
// order partyGetOrderMember() iterates, starting at index 0 (see
// cutsceneStartBattleStart).
// order config->players iterates (see cutsceneStartBattleStart).
uint8_t allySlot = 0;
for(uint8_t i = 0; i < PARTY_ACTIVE_SIZE_MAX; i++) {
battlefighter_t *member = partyGetOrderMember(i);
for(uint8_t i = 0; i < config->playerCount; i++) {
battlefighter_t *member =
partyGetOrderMember(config->players[i]);
if(member == NULL) continue;
battlefighter_t *fighter = &BATTLE.fighters[allySlot++];
@@ -74,9 +84,7 @@ bool_t cutsceneStartBattleUpdate(
errorret_t cutsceneStartBattleLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
battleencountertype_t encounterValue;
if(!cutsceneJsonLookupBattleEncounter(
@@ -89,6 +97,38 @@ errorret_t cutsceneStartBattleLoad(
item->startBattle.fleeAvailable =
cutsceneJsonGetBool(itemObj, "fleeAvailable", true);
yyjson_val *players = yyjson_obj_get(itemObj, "players");
if(players == NULL) {
item->startBattle.playerCount = PARTY_ACTIVE_SIZE_MAX;
for(uint8_t i = 0; i < PARTY_ACTIVE_SIZE_MAX; i++) {
item->startBattle.players[i] = i;
}
} else {
size_t playerCount = yyjson_arr_size(players);
if(!yyjson_is_arr(players) || playerCount > PARTY_ACTIVE_SIZE_MAX) {
errorThrow(
"Cutscene item (START_BATTLE): 'players' must have 0-%u entries",
(uint32_t)PARTY_ACTIVE_SIZE_MAX
);
}
item->startBattle.playerCount = (uint8_t)playerCount;
yyjson_arr_iter playerIter = yyjson_arr_iter_with(players);
yyjson_val *playerVal;
size_t p = 0;
while((playerVal = yyjson_arr_iter_next(&playerIter)) != NULL) {
int64_t index = yyjson_get_int(playerVal);
if(index < 0 || index >= PARTY_ACTIVE_SIZE_MAX) {
errorThrow(
"Cutscene item (START_BATTLE): players[%u] must be 0-%u",
(uint32_t)p, (uint32_t)(PARTY_ACTIVE_SIZE_MAX - 1)
);
}
item->startBattle.players[p] = (uint8_t)index;
p++;
}
}
yyjson_val *enemies = yyjson_obj_get(itemObj, "enemies");
size_t enemyCount = yyjson_arr_size(enemies);
if(!yyjson_is_arr(enemies) || enemyCount == 0 ||
@@ -126,6 +166,37 @@ errorret_t cutsceneStartBattleLoad(
(uint16_t)yyjson_get_int(yyjson_obj_get(enemyVal, "healthMax"));
enemy->mpMax =
(uint16_t)yyjson_get_int(yyjson_obj_get(enemyVal, "mpMax"));
yyjson_val *abilities = yyjson_obj_get(enemyVal, "abilities");
if(abilities != NULL) {
size_t abilityCount = yyjson_arr_size(abilities);
if(!yyjson_is_arr(abilities) ||
abilityCount > BATTLE_FIGHTER_ABILITY_COUNT_MAX) {
errorThrow(
"Cutscene item (START_BATTLE): enemies[%u].abilities must have "
"0-%u entries",
(uint32_t)e, (uint32_t)BATTLE_FIGHTER_ABILITY_COUNT_MAX
);
}
yyjson_arr_iter abilityIter = yyjson_arr_iter_with(abilities);
yyjson_val *abilityVal;
size_t a = 0;
while((abilityVal = yyjson_arr_iter_next(&abilityIter)) != NULL) {
battleability_t ability;
if(!cutsceneJsonLookupBattleAbility(
yyjson_get_str(abilityVal), &ability
)) {
errorThrow(
"Cutscene item (START_BATTLE): enemies[%u].abilities[%u] is "
"invalid",
(uint32_t)e, (uint32_t)a
);
}
enemy->abilities[a] = ability;
a++;
}
}
e++;
}
@@ -7,6 +7,7 @@
#pragma once
#include "rpg/battle/battle.h"
#include "rpg/battle/party.h"
#include "rpg/cutscene/item/cutsceneitembase.h"
#define CUTSCENE_START_BATTLE_ENEMY_COUNT_MAX 4
@@ -15,6 +16,7 @@ typedef struct {
battlefighterstats_t stats;
uint16_t healthMax;
uint16_t mpMax;
battleability_t abilities[BATTLE_FIGHTER_ABILITY_COUNT_MAX];
} cutscenestartbattleenemy_t;
typedef struct {
@@ -22,6 +24,13 @@ typedef struct {
bool_t fleeAvailable;
uint8_t enemyCount;
cutscenestartbattleenemy_t enemies[CUTSCENE_START_BATTLE_ENEMY_COUNT_MAX];
// Active party order slots (see party.h) to bring into the battle, in
// the order they should be added. Defaults to every active slot, 0
// through PARTY_ACTIVE_SIZE_MAX - 1 in order, when the item's JSON
// omits "players" - see cutsceneStartBattleLoad.
uint8_t players[PARTY_ACTIVE_SIZE_MAX];
uint8_t playerCount;
} cutscenestartbattle_t;
/**
@@ -55,13 +64,9 @@ bool_t cutsceneStartBattleUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (START_BATTLE needs no pool storage).
* @param poolOffset Unused (START_BATTLE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneStartBattleLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -22,9 +22,7 @@ bool_t cutsceneMarkerUpdate(
errorret_t cutsceneMarkerLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
const char_t *name = yyjson_get_str(yyjson_obj_get(itemObj, "name"));
if(name == NULL) {
@@ -41,13 +41,9 @@ bool_t cutsceneMarkerUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (MARKER needs no pool storage).
* @param poolOffset Unused (MARKER needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneMarkerLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneSceneUpdate(
errorret_t cutsceneSceneLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
scenetype_t sceneValue;
if(!cutsceneJsonLookupSceneType(
@@ -45,13 +45,9 @@ bool_t cutsceneSceneUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (SCENE needs no pool storage).
* @param poolOffset Unused (SCENE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneSceneLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneSetPauseUpdate(
errorret_t cutsceneSetPauseLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
yyjson_val *flags = yyjson_obj_get(itemObj, "flags");
cutscenepause_t value;
@@ -37,13 +37,9 @@ bool_t cutsceneSetPauseUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (SET_PAUSE needs no pool storage).
* @param poolOffset Unused (SET_PAUSE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneSetPauseLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -26,9 +26,7 @@ bool_t cutsceneWaitUpdate(
errorret_t cutsceneWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
item->wait = cutsceneJsonGetFloat(itemObj, "seconds", 0.0f);
@@ -39,13 +39,9 @@ bool_t cutsceneWaitUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (WAIT needs no pool storage).
* @param poolOffset Unused (WAIT needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
+156 -22
View File
@@ -10,7 +10,9 @@
#include "asset/asset.h"
#include "asset/assetfile.h"
#include "asset/loader/assetloader.h"
#include "util/memory.h"
#include "util/string.h"
#include "ui/overlay/uifatalerror.h"
cutsceneitemcallbacks_t CUTSCENE_ITEM_CALLBACKS[CUTSCENE_ITEM_TYPE_COUNT] = {
[CUTSCENE_ITEM_TYPE_NULL] = { 0 },
@@ -66,8 +68,7 @@ cutsceneitemcallbacks_t CUTSCENE_ITEM_CALLBACKS[CUTSCENE_ITEM_TYPE_COUNT] = {
.init = cutsceneEntityWalkToStart,
.update = cutsceneEntityWalkToUpdate,
.jsonName = "ENTITY_WALK_TO",
.load = cutsceneEntityWalkToLoad,
.poolSize = cutsceneEntityWalkToPoolSize
.load = cutsceneEntityWalkToLoad
},
[CUTSCENE_ITEM_TYPE_FADE] = {
@@ -140,8 +141,7 @@ cutsceneitemcallbacks_t CUTSCENE_ITEM_CALLBACKS[CUTSCENE_ITEM_TYPE_COUNT] = {
.init = cutsceneMapAreaWaitStart,
.update = cutsceneMapAreaWaitUpdate,
.jsonName = "MAP_AREA_WAIT",
.load = cutsceneMapAreaWaitLoad,
.poolSize = cutsceneMapAreaWaitPoolSize
.load = cutsceneMapAreaWaitLoad
},
[CUTSCENE_ITEM_TYPE_START_BATTLE] = {
@@ -326,6 +326,19 @@ cutsceneitemcallbacks_t CUTSCENE_ITEM_CALLBACKS[CUTSCENE_ITEM_TYPE_COUNT] = {
.update = cutsceneUIShowUpdate,
.jsonName = "UI_SHOW",
.load = cutsceneUIShowLoad
},
[CUTSCENE_ITEM_TYPE_REGULAR_BATTLE] = {
.update = cutsceneRegularBattleUpdate,
.jsonName = "REGULAR_BATTLE",
.load = cutsceneItemLoadNoop
},
[CUTSCENE_ITEM_TYPE_INSERT] = {
.init = cutsceneInsertStart,
.update = cutsceneInsertUpdate,
.jsonName = "INSERT",
.load = cutsceneInsertLoad
}
};
@@ -364,7 +377,17 @@ void cutsceneCutsceneStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
if(item->cutscene != NULL) cutsceneSystemStartCutscene(item->cutscene);
const cutscene_t *cutscene = item->cutsceneRef.cutscene;
if(cutscene == NULL && item->cutsceneRef.name != NULL) {
// Not cached back into item - with only one shared, fixed load buffer
// (see cutscenesystem.h's CUTSCENE_LOADED_ITEMS_MAX doc comment),
// every JSON-authored reference resolves to the same fixed address
// every time anyway, and item itself lives inside that very buffer -
// caching a pointer into it here would go stale (or, worse, get
// clobbered mid-resolve) the moment a later load overwrites it.
cutscene = cutsceneCutsceneResolve(item->cutsceneRef.name);
}
if(cutscene != NULL) cutsceneSystemStartCutscene(cutscene);
}
bool_t cutsceneCutsceneUpdate(
@@ -374,34 +397,145 @@ bool_t cutsceneCutsceneUpdate(
return false;
}
const cutscene_t * cutsceneCutsceneResolve(const char_t *name) {
char_t path[ASSET_FILE_NAME_MAX];
stringFormat(path, sizeof(path), "cutscenes/%s.jsonc", name);
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_JSON, NULL);
errorret_t result = assetRequireLoaded(entry);
if(errorIsNotOk(result)) {
assetUnlockEntry(entry);
errorCatch(errorPrint(result));
uiFatalErrorOpen(NULL);
return NULL;
}
// Steal the parsed doc before unlocking - assetJsonDispose would
// otherwise free it out from under CUTSCENE_LOADED_ITEMS' name pointers
// the moment this entry gets reaped.
yyjson_doc *doc = entry->data.json;
entry->data.json = NULL;
assetUnlockEntry(entry);
// Force this now-zero-ref entry to actually go away right away, rather
// than leaving it languishing as LOADED until something else happens to
// trigger a reap - assetRequireLoaded treats an already-LOADED entry as
// an instant cache hit with no reparsing, so a stale leftover entry for
// this same path would otherwise make a later resolve of it wrongly
// skip loading, still returning CUTSCENE_LOADED_SCENE even after some
// other cutscene has since overwritten it.
assetReapUnused();
errorret_t parseResult = cutsceneParseDoc(
doc, &CUTSCENE_LOADED_SCENE, CUTSCENE_LOADED_ITEMS,
CUTSCENE_LOADED_ITEMS_MAX
);
if(errorIsNotOk(parseResult)) {
yyjson_doc_free(doc);
errorCatch(errorPrint(parseResult));
uiFatalErrorOpen(NULL);
return NULL;
}
cutsceneLoadedSetDoc(doc);
return &CUTSCENE_LOADED_SCENE;
}
errorret_t cutsceneCutsceneLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
return cutsceneCutsceneRefLoad(itemObj, &item->cutsceneRef, "CUTSCENE");
}
errorret_t cutsceneCutsceneRefLoad(
yyjson_val *itemObj,
cutscenecutsceneref_t *outRef,
const char_t *itemTypeLabel
) {
const char_t *name = yyjson_get_str(yyjson_obj_get(itemObj, "name"));
if(name == NULL) {
errorThrow("Cutscene item (CUTSCENE): missing 'name'");
errorThrow("Cutscene item (%s): missing 'name'", itemTypeLabel);
}
// Referenced cutscene is a sibling asset (assets/cutscenes/<name>.jsonc),
// resolved to a stable pointer right now rather than deferred to
// runtime - assetRequireLoaded re-enters assetUpdate() safely from
// within this sync load (see its ASSET_ENTRY_STATE_LOADING_SYNC case),
// and the entry is locked forever (never unlocked) so the pointer
// stays valid for the process lifetime, same convention as
// ui/screen/mainmenu/uimainmenu.c's MAIN_MENU_CUTSCENE_ENTRY.
char_t path[ASSET_FILE_NAME_MAX];
// Only validate that the eventual "cutscenes/<name>.jsonc" path would
// fit - the reference itself is not resolved/locked until this item
// actually runs (see cutsceneCutsceneStart/cutsceneCutsceneResolve).
int32_t pathLen = stringFormat(NULL, 0, "cutscenes/%s.jsonc", name);
if(pathLen < 0 || (size_t)pathLen >= ASSET_FILE_NAME_MAX) {
errorThrow("Cutscene item (CUTSCENE): 'name' too long");
errorThrow("Cutscene item (%s): 'name' too long", itemTypeLabel);
}
stringFormat(path, sizeof(path), "cutscenes/%s.jsonc", name);
assetentry_t *refEntry = assetLock(path, ASSET_LOADER_TYPE_CUTSCENE, NULL);
errorChain(assetRequireLoaded(refEntry));
item->cutscene = &refEntry->data.cutscene.cutscene;
outRef->cutscene = NULL;
outRef->name = name;
errorOk();
}
void cutsceneInsertStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
const cutscene_t *cutscene = item->insert.cutscene;
if(cutscene == NULL && item->insert.name != NULL) {
// Not cached back into item - same reasoning as cutsceneCutsceneStart,
// but resolved via cutsceneInsertResolve (the dedicated INSERT buffer)
// rather than cutsceneCutsceneResolve, since item itself may live
// inside the currently-running outer cutscene's own CUTSCENE_LOADED_*
// buffer and must not be clobbered by this resolve.
cutscene = cutsceneInsertResolve(item->insert.name);
}
if(cutscene != NULL) cutsceneSystemInsertCutscene(cutscene);
}
bool_t cutsceneInsertUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
return false;
}
const cutscene_t * cutsceneInsertResolve(const char_t *name) {
char_t path[ASSET_FILE_NAME_MAX];
stringFormat(path, sizeof(path), "cutscenes/%s.jsonc", name);
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_JSON, NULL);
errorret_t result = assetRequireLoaded(entry);
if(errorIsNotOk(result)) {
assetUnlockEntry(entry);
errorCatch(errorPrint(result));
uiFatalErrorOpen(NULL);
return NULL;
}
// Steal the parsed doc before unlocking - assetJsonDispose would
// otherwise free it out from under CUTSCENE_INSERT_ITEMS' name pointers
// the moment this entry gets reaped.
yyjson_doc *doc = entry->data.json;
entry->data.json = NULL;
assetUnlockEntry(entry);
// See cutsceneCutsceneResolve's matching comment - forces a stale
// leftover entry for this same path out immediately, so a later resolve
// never wrongly skips reloading it.
assetReapUnused();
errorret_t parseResult = cutsceneParseDoc(
doc, &CUTSCENE_INSERT_SCENE, CUTSCENE_INSERT_ITEMS,
CUTSCENE_INSERT_ITEMS_MAX
);
if(errorIsNotOk(parseResult)) {
yyjson_doc_free(doc);
errorCatch(errorPrint(parseResult));
uiFatalErrorOpen(NULL);
return NULL;
}
cutsceneInsertSetDoc(doc);
return &CUTSCENE_INSERT_SCENE;
}
errorret_t cutsceneInsertLoad(
yyjson_val *itemObj,
cutsceneitem_t *item
) {
return cutsceneCutsceneRefLoad(itemObj, &item->insert, "INSERT");
}
+139 -75
View File
@@ -8,6 +8,7 @@
#pragma once
#include "error/error.h"
#include "yyjson.h"
#include "cutsceneitembase.h"
#include "cutscenecallback.h"
#include "cutsceneprint.h"
#include "control/cutscenewait.h"
@@ -41,6 +42,7 @@
#include "battle/cutscenestartbattle.h"
#include "battle/cutscenebattlewaitstate.h"
#include "battle/cutscenebattleforceaction.h"
#include "battle/cutsceneregularbattle.h"
#include "save/cutscenesavedevicecheck.h"
#include "save/cutscenesaveloadallslots.h"
#include "audio/cutsceneaudioplay.h"
@@ -55,58 +57,29 @@
typedef struct cutscene_s cutscene_t;
typedef enum {
CUTSCENE_ITEM_TYPE_NULL,
CUTSCENE_ITEM_TYPE_TEXT,
CUTSCENE_ITEM_TYPE_TEXT_MINI,
CUTSCENE_ITEM_TYPE_TEXT_MINI_HIDE,
CUTSCENE_ITEM_TYPE_CALLBACK,
CUTSCENE_ITEM_TYPE_WAIT,
CUTSCENE_ITEM_TYPE_CUTSCENE,
CUTSCENE_ITEM_TYPE_ENTITY_TELEPORT,
CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO,
CUTSCENE_ITEM_TYPE_FADE,
CUTSCENE_ITEM_TYPE_SET_PAUSE,
CUTSCENE_ITEM_TYPE_CONCURRENT,
CUTSCENE_ITEM_TYPE_ITEM_GIVE,
CUTSCENE_ITEM_TYPE_ENTITY_REMOVE,
CUTSCENE_ITEM_TYPE_ENTITY_ADD,
CUTSCENE_ITEM_TYPE_ENTITY_TURN,
CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO_ENTITY,
CUTSCENE_ITEM_TYPE_MAP_AREA_ADD,
CUTSCENE_ITEM_TYPE_MAP_AREA_REMOVE,
CUTSCENE_ITEM_TYPE_MAP_AREA_WAIT,
CUTSCENE_ITEM_TYPE_START_BATTLE,
CUTSCENE_ITEM_TYPE_EMOJI,
CUTSCENE_ITEM_TYPE_SHAKE,
CUTSCENE_ITEM_TYPE_BATTLE_WAIT_STATE,
CUTSCENE_ITEM_TYPE_BATTLE_FORCE_ACTION,
CUTSCENE_ITEM_TYPE_MODAL,
CUTSCENE_ITEM_TYPE_MODAL_OPTIONS_MARKERS,
CUTSCENE_ITEM_TYPE_MODAL_CLOSE,
CUTSCENE_ITEM_TYPE_PRINT,
CUTSCENE_ITEM_TYPE_MARKER,
CUTSCENE_ITEM_TYPE_RESTART,
CUTSCENE_ITEM_TYPE_QUIT_GAME,
CUTSCENE_ITEM_TYPE_SCENE,
CUTSCENE_ITEM_TYPE_SAVE_DEVICE_CHECK,
CUTSCENE_ITEM_TYPE_SAVE_LOAD_ALL_SLOTS,
CUTSCENE_ITEM_TYPE_KEYBOARD,
CUTSCENE_ITEM_TYPE_AUDIO_PLAY,
CUTSCENE_ITEM_TYPE_AUDIO_STOP,
CUTSCENE_ITEM_TYPE_AUDIO_PAUSE,
CUTSCENE_ITEM_TYPE_AUDIO_RESUME,
CUTSCENE_ITEM_TYPE_AUDIO_FADE,
CUTSCENE_ITEM_TYPE_AUDIO_FADE_WAIT,
CUTSCENE_ITEM_TYPE_AUDIO_SET_PAN,
CUTSCENE_ITEM_TYPE_AUDIO_SET_LOOP,
CUTSCENE_ITEM_TYPE_AUDIO_SET,
CUTSCENE_ITEM_TYPE_IDLE,
CUTSCENE_ITEM_TYPE_UI_SHOW,
CUTSCENE_ITEM_TYPE_COUNT
} cutsceneitemtype_t;
// A CUTSCENE (or INSERT) item's target. C-authored cutscenes (the
// CUTSCENE_CUTSCENE/CUTSCENE_INSERT macros in cutscene.h) resolve cutscene
// at compile time and leave name NULL - safe to hold onto indefinitely,
// since C-authored cutscenes are compiled directly into their own static
// const arrays, never a shared load buffer. JSON-authored cutscenes
// (cutsceneCutsceneLoad/cutsceneInsertLoad) do the opposite - only name is
// set at parse time, since resolving/locking the referenced asset there
// would eagerly load every cutscene a cutscene merely references, even
// ones never played. cutsceneCutsceneStart/cutsceneInsertStart resolve
// name to cutscene the first time this item actually runs (via
// cutsceneCutsceneResolve/cutsceneInsertResolve respectively) but do NOT
// cache the result back into this field - with only one shared, fixed
// buffer per destination (see cutscenesystem.h), every JSON-authored
// reference resolves to the same fixed address every time anyway, and
// item itself may live inside that very buffer, so caching a pointer into
// it here would go stale (or, worse, get clobbered mid-resolve) the
// moment a later load overwrites it. Shared by both CUTSCENE
// (item->cutsceneRef) and INSERT (item->insert), since both need exactly
// this "resolved pointer, or a name to lazily resolve" shape.
typedef struct {
const cutscene_t *cutscene;
const char_t *name;
} cutscenecutsceneref_t;
struct cutsceneitem_s {
cutsceneitemtype_t type;
@@ -117,7 +90,8 @@ struct cutsceneitem_s {
cutscenetextminihide_t textMiniHide;
cutscenecallback_t callback;
cutscenewait_t wait;
const cutscene_t *cutscene;
cutscenecutsceneref_t cutsceneRef;
cutscenecutsceneref_t insert;
cutsceneentityteleport_t entityTeleport;
cutsceneentitywalkto_t entityWalkTo;
cutscenefade_t fade;
@@ -176,22 +150,14 @@ typedef bool_t (cutsceneitemupdatecallback_t)(
typedef errorret_t (cutsceneitemloadcallback_t)(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
// Returns how many pool bytes (see cutsceneJsonPoolAlign) this JSON item
// needs - only ENTITY_WALK_TO/MAP_AREA_WAIT need this; every other type's
// poolSize is NULL (0 bytes).
typedef size_t (cutsceneitempoolsizecallback_t)(yyjson_val *itemObj);
typedef struct {
cutsceneiteminitcallback_t *init;
cutsceneitemupdatecallback_t *update;
const char_t *jsonName; // NULL if not JSON-authorable
cutsceneitemloadcallback_t *load; // NULL if not JSON-authorable
cutsceneitempoolsizecallback_t *poolSize; // NULL if this type needs no pool
const char_t *jsonName; // NULL if not JSON-authorable
cutsceneitemloadcallback_t *load; // NULL if not JSON-authorable
} cutsceneitemcallbacks_t;
extern cutsceneitemcallbacks_t
@@ -234,8 +200,11 @@ bool_t cutsceneItemLookupType(
);
/**
* Starts a nested-cutscene item, handing control over to the
* referenced cutscene.
* Starts a nested-cutscene item, handing control over to the referenced
* cutscene. If item->cutsceneRef.cutscene hasn't been resolved yet (a
* JSON-authored reference, only known by name so far), resolves it first
* via cutsceneCutsceneResolve (not cached back - see cutscenecutsceneref_t's
* doc comment for why).
*
* @param item The cutscene item.
* @param data Runtime data storage.
@@ -245,6 +214,22 @@ void cutsceneCutsceneStart(
cutsceneitemdata_t *data
);
/**
* Resolves a JSON-authored CUTSCENE item's referenced cutscene by name,
* locking (forever - never unlocked, matching cutsceneCutsceneLoad's old
* convention) and blocking on assets/cutscenes/<name>.jsonc. Called
* lazily from cutsceneCutsceneStart the first time the item actually
* runs, not at parse time, so a cutscene referencing many others doesn't
* load every one of them up front. On failure, opens the fatal error
* overlay (matching cutsceneSystemLoad's runtime-failure handling) and
* returns NULL.
*
* @param name Bare cutscene name (without "cutscenes/" prefix or
* ".jsonc" suffix).
* @return The resolved cutscene, or NULL on failure.
*/
const cutscene_t * cutsceneCutsceneResolve(const char_t *name);
/**
* Updates a nested-cutscene item. By the time this would run, control
* has already moved on to the referenced cutscene, so this always
@@ -262,20 +247,99 @@ bool_t cutsceneCutsceneUpdate(
/**
* Parses a CUTSCENE cutscene item's JSON fields into item. Kept inline here
* (rather than its own file) since it's tightly bound to the asset/loader
* system itself - it resolves its 'name' to a sibling
* assets/cutscenes/<name>.jsonc asset at parse time, mirroring how
* cutsceneCutsceneStart/cutsceneCutsceneUpdate stay inline in
* cutsceneitem.c rather than getting their own file.
* system itself. Only stores the 'name' string (which points into the
* cutscene asset's still-alive yyjson doc, so no copy is needed) -
* resolving it to the referenced assets/cutscenes/<name>.jsonc asset is
* deferred to cutsceneCutsceneStart, the first time this item actually
* runs, so a cutscene referencing many others doesn't pay to load every
* one of them up front.
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (CUTSCENE needs no pool storage).
* @param poolOffset Unused (CUTSCENE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneCutsceneLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
/**
* Shared JSON parsing body for both CUTSCENE and INSERT items - both need
* exactly a 'name' string field, validated to fit the eventual
* "cutscenes/<name>.jsonc" asset path. Only stores the name (see
* cutsceneCutsceneLoad for why resolving is deferred).
*
* @param itemObj The item's JSON object.
* @param outRef Destination ref, already zeroed by the caller.
* @param itemTypeLabel Item type name used only in the error message on
* failure (e.g. "CUTSCENE" or "INSERT").
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneCutsceneRefLoad(
yyjson_val *itemObj,
cutscenecutsceneref_t *outRef,
const char_t *itemTypeLabel
);
/**
* Starts an INSERT item, splicing the referenced cutscene's items into the
* running cutscene in place - unlike CUTSCENE (a one-way jump that replaces
* the running cutscene outright), control returns to the item right after
* this one once the inserted items run out. If item->insert.cutscene hasn't
* been resolved yet (a JSON-authored reference, only known by name so
* far), resolves it first via cutsceneInsertResolve - not
* cutsceneCutsceneResolve, so the referenced cutscene lands in the
* dedicated CUTSCENE_INSERT_ITEMS buffer rather than overwriting
* CUTSCENE_LOADED_ITEMS out from under whatever outer cutscene is
* currently running (see cutscenesystem.h's CUTSCENE_INSERT_ITEMS_MAX doc
* comment). Not cached back into item, same reasoning as
* cutsceneCutsceneStart.
*
* @param item The cutscene item.
* @param data Runtime data storage.
*/
void cutsceneInsertStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
);
/**
* Resolves a JSON-authored INSERT item's referenced cutscene by name -
* locks+loads assets/cutscenes/<name>.jsonc (as a plain
* ASSET_LOADER_TYPE_JSON asset, same path/key convention as
* cutsceneCutsceneResolve - which destination buffer it lands in is
* decided purely by which resolve function is called, not stored on the
* asset entry itself), then parses it into CUTSCENE_INSERT_ITEMS rather
* than CUTSCENE_LOADED_ITEMS. On failure, opens the fatal error overlay
* and returns NULL.
*
* @param name Bare cutscene name (without "cutscenes/" prefix or
* ".jsonc" suffix).
* @return The resolved cutscene, or NULL on failure.
*/
const cutscene_t * cutsceneInsertResolve(const char_t *name);
/**
* Updates an INSERT item. By the time this would run, control has already
* moved on to the inserted items, so this always reports incomplete.
*
* @param item The cutscene item.
* @param data Runtime data storage.
* @returns false always.
*/
bool_t cutsceneInsertUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
);
/**
* Parses an INSERT cutscene item's JSON fields into item.
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneInsertLoad(
yyjson_val *itemObj,
cutsceneitem_t *item
);
@@ -9,9 +9,7 @@
errorret_t cutsceneItemLoadNoop(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
errorOk();
}
+56 -5
View File
@@ -13,6 +13,61 @@
typedef struct cutsceneitem_s cutsceneitem_t;
typedef union cutsceneitemdata_u cutsceneitemdata_t;
typedef enum {
CUTSCENE_ITEM_TYPE_NULL,
CUTSCENE_ITEM_TYPE_TEXT,
CUTSCENE_ITEM_TYPE_TEXT_MINI,
CUTSCENE_ITEM_TYPE_TEXT_MINI_HIDE,
CUTSCENE_ITEM_TYPE_CALLBACK,
CUTSCENE_ITEM_TYPE_WAIT,
CUTSCENE_ITEM_TYPE_CUTSCENE,
CUTSCENE_ITEM_TYPE_ENTITY_TELEPORT,
CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO,
CUTSCENE_ITEM_TYPE_FADE,
CUTSCENE_ITEM_TYPE_SET_PAUSE,
CUTSCENE_ITEM_TYPE_CONCURRENT,
CUTSCENE_ITEM_TYPE_ITEM_GIVE,
CUTSCENE_ITEM_TYPE_ENTITY_REMOVE,
CUTSCENE_ITEM_TYPE_ENTITY_ADD,
CUTSCENE_ITEM_TYPE_ENTITY_TURN,
CUTSCENE_ITEM_TYPE_ENTITY_WALK_TO_ENTITY,
CUTSCENE_ITEM_TYPE_MAP_AREA_ADD,
CUTSCENE_ITEM_TYPE_MAP_AREA_REMOVE,
CUTSCENE_ITEM_TYPE_MAP_AREA_WAIT,
CUTSCENE_ITEM_TYPE_START_BATTLE,
CUTSCENE_ITEM_TYPE_EMOJI,
CUTSCENE_ITEM_TYPE_SHAKE,
CUTSCENE_ITEM_TYPE_BATTLE_WAIT_STATE,
CUTSCENE_ITEM_TYPE_BATTLE_FORCE_ACTION,
CUTSCENE_ITEM_TYPE_MODAL,
CUTSCENE_ITEM_TYPE_MODAL_OPTIONS_MARKERS,
CUTSCENE_ITEM_TYPE_MODAL_CLOSE,
CUTSCENE_ITEM_TYPE_PRINT,
CUTSCENE_ITEM_TYPE_MARKER,
CUTSCENE_ITEM_TYPE_RESTART,
CUTSCENE_ITEM_TYPE_QUIT_GAME,
CUTSCENE_ITEM_TYPE_SCENE,
CUTSCENE_ITEM_TYPE_SAVE_DEVICE_CHECK,
CUTSCENE_ITEM_TYPE_SAVE_LOAD_ALL_SLOTS,
CUTSCENE_ITEM_TYPE_KEYBOARD,
CUTSCENE_ITEM_TYPE_AUDIO_PLAY,
CUTSCENE_ITEM_TYPE_AUDIO_STOP,
CUTSCENE_ITEM_TYPE_AUDIO_PAUSE,
CUTSCENE_ITEM_TYPE_AUDIO_RESUME,
CUTSCENE_ITEM_TYPE_AUDIO_FADE,
CUTSCENE_ITEM_TYPE_AUDIO_FADE_WAIT,
CUTSCENE_ITEM_TYPE_AUDIO_SET_PAN,
CUTSCENE_ITEM_TYPE_AUDIO_SET_LOOP,
CUTSCENE_ITEM_TYPE_AUDIO_SET,
CUTSCENE_ITEM_TYPE_IDLE,
CUTSCENE_ITEM_TYPE_UI_SHOW,
CUTSCENE_ITEM_TYPE_REGULAR_BATTLE,
CUTSCENE_ITEM_TYPE_INSERT,
CUTSCENE_ITEM_TYPE_COUNT
} cutsceneitemtype_t;
/**
* Shared no-op Load for item types with no JSON fields of their own to
* parse (IDLE, RESTART, MODAL_CLOSE) - always succeeds without touching
@@ -20,13 +75,9 @@ typedef union cutsceneitemdata_u cutsceneitemdata_t;
*
* @param itemObj The item's JSON object (unused).
* @param item Destination item (unused).
* @param pool Unused.
* @param poolOffset Unused.
* @return Always errorOk().
*/
errorret_t cutsceneItemLoadNoop(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
+1 -3
View File
@@ -25,9 +25,7 @@ bool_t cutscenePrintUpdate(
errorret_t cutscenePrintLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonCopyString(
itemObj, "text", NULL, item->print.text, CUTSCENE_PRINT_MAX_CHARS
+1 -5
View File
@@ -42,13 +42,9 @@ bool_t cutscenePrintUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (PRINT needs no pool storage).
* @param poolOffset Unused (PRINT needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutscenePrintLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -38,9 +38,7 @@ bool_t cutsceneEntityAddUpdate(
errorret_t cutsceneEntityAddLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
entitytype_t entityTypeValue;
if(!cutsceneJsonLookupEntityType(
@@ -43,13 +43,9 @@ bool_t cutsceneEntityAddUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ENTITY_ADD needs no pool storage).
* @param poolOffset Unused (ENTITY_ADD needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityAddLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -29,9 +29,7 @@ bool_t cutsceneEntityRemoveUpdate(
errorret_t cutsceneEntityRemoveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetEntityIndex(
yyjson_obj_get(itemObj, "entityIndex"), &item->entityRemove.entityIndex
@@ -40,13 +40,9 @@ bool_t cutsceneEntityRemoveUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ENTITY_REMOVE needs no pool storage).
* @param poolOffset Unused (ENTITY_REMOVE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityRemoveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -30,9 +30,7 @@ bool_t cutsceneEntityTeleportUpdate(
errorret_t cutsceneEntityTeleportLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetEntityIndex(
yyjson_obj_get(itemObj, "entityIndex"),
@@ -42,13 +42,9 @@ bool_t cutsceneEntityTeleportUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ENTITY_TELEPORT needs no pool storage).
* @param poolOffset Unused (ENTITY_TELEPORT needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityTeleportLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -33,9 +33,7 @@ bool_t cutsceneEntityTurnUpdate(
errorret_t cutsceneEntityTurnLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetEntityIndex(
yyjson_obj_get(itemObj, "entityIndex"), &item->entityTurn.entityIndex
@@ -44,13 +44,9 @@ bool_t cutsceneEntityTurnUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ENTITY_TURN needs no pool storage).
* @param poolOffset Unused (ENTITY_TURN needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityTurnLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -10,12 +10,16 @@
#include "rpg/entity/entitypathstep.h"
#include "rpg/cutscene/item/json/cutscenejsonentityindex.h"
#include "rpg/cutscene/item/json/cutscenejsonfield.h"
#include "rpg/cutscene/item/json/cutscenejsonpool.h"
#include "assert/assert.h"
void cutsceneEntityWalkToStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
assertTrue(
item->entityWalkTo.count <= CUTSCENE_ENTITY_WALK_TO_MAX,
"Too many waypoints in CUTSCENE_ENTITY_WALK_TO"
);
data->entityWalkTo.currentIndex = 0;
}
@@ -40,9 +44,7 @@ bool_t cutsceneEntityWalkToUpdate(
errorret_t cutsceneEntityWalkToLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetEntityIndex(
yyjson_obj_get(itemObj, "entityIndex"), &item->entityWalkTo.entityIndex
@@ -54,20 +56,22 @@ errorret_t cutsceneEntityWalkToLoad(
yyjson_val *positions = yyjson_obj_get(itemObj, "positions");
size_t count = yyjson_arr_size(positions);
if(!yyjson_is_arr(positions) || count == 0 || count > UINT8_MAX) {
if(
!yyjson_is_arr(positions) || count == 0 ||
count > CUTSCENE_ENTITY_WALK_TO_MAX
) {
errorThrow(
"Cutscene item (ENTITY_WALK_TO): 'positions' must be a non-empty "
"array of [x, y, z]"
"Cutscene item (ENTITY_WALK_TO): 'positions' must have 1-%u entries "
"of [x, y, z]",
(uint32_t)CUTSCENE_ENTITY_WALK_TO_MAX
);
}
*poolOffset = cutsceneJsonPoolAlign(*poolOffset);
worldpos_t *dest = (worldpos_t *)(pool + *poolOffset);
yyjson_arr_iter iter = yyjson_arr_iter_with(positions);
yyjson_val *posVal;
size_t i = 0;
while((posVal = yyjson_arr_iter_next(&iter)) != NULL) {
if(!cutsceneJsonReadWorldPos(posVal, &dest[i])) {
if(!cutsceneJsonReadWorldPos(posVal, &item->entityWalkTo.positions[i])) {
errorThrow(
"Cutscene item (ENTITY_WALK_TO): position %u must be [x, y, z]",
(uint32_t)i
@@ -75,15 +79,7 @@ errorret_t cutsceneEntityWalkToLoad(
}
i++;
}
item->entityWalkTo.positions = dest;
item->entityWalkTo.count = (uint8_t)count;
*poolOffset += count * sizeof(worldpos_t);
errorOk();
}
size_t cutsceneEntityWalkToPoolSize(yyjson_val *itemObj) {
yyjson_val *positions = yyjson_obj_get(itemObj, "positions");
if(!yyjson_is_arr(positions)) return 0;
return yyjson_arr_size(positions) * sizeof(worldpos_t);
}
@@ -9,9 +9,14 @@
#include "rpg/overworld/worldpos.h"
#include "rpg/cutscene/item/cutsceneitembase.h"
// Maximum number of waypoints a single ENTITY_WALK_TO/
// CUTSCENE_ENTITY_WALK_PATH may hold - generous relative to every current
// asset/C-authored cutscene (none use more than a couple waypoints).
#define CUTSCENE_ENTITY_WALK_TO_MAX 16
typedef struct {
uint8_t entityIndex;
const worldpos_t *positions;
worldpos_t positions[CUTSCENE_ENTITY_WALK_TO_MAX];
uint8_t count;
bool_t walkAround;
} cutsceneentitywalkto_t;
@@ -45,26 +50,14 @@ bool_t cutsceneEntityWalkToUpdate(
/**
* Parses an ENTITY_WALK_TO cutscene item's JSON fields into item, writing
* its "positions" array into pool at *poolOffset and advancing it.
* its "positions" array directly into item->entityWalkTo.positions (at
* most CUTSCENE_ENTITY_WALK_TO_MAX entries).
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Base pointer of the entry's pool allocation.
* @param poolOffset In/out cursor into pool.
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityWalkToLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
/**
* Computes how many pool bytes this ENTITY_WALK_TO item's "positions"
* array will need.
*
* @param itemObj The item's JSON object.
* @return Pool bytes required (0 if "positions" is missing/not an array).
*/
size_t cutsceneEntityWalkToPoolSize(yyjson_val *itemObj);
@@ -43,9 +43,7 @@ bool_t cutsceneEntityWalkToEntityUpdate(
errorret_t cutsceneEntityWalkToEntityLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetEntityIndex(
yyjson_obj_get(itemObj, "entityIndex"),
@@ -48,13 +48,9 @@ bool_t cutsceneEntityWalkToEntityUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ENTITY_WALK_TO_ENTITY needs no pool storage).
* @param poolOffset Unused (ENTITY_WALK_TO_ENTITY needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneEntityWalkToEntityLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -25,9 +25,7 @@ bool_t cutsceneItemGiveUpdate(
errorret_t cutsceneItemGiveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
item->itemGive.item =
(itemid_t)yyjson_get_int(yyjson_obj_get(itemObj, "item"));
@@ -42,13 +42,9 @@ bool_t cutsceneItemGiveUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (ITEM_GIVE needs no pool storage).
* @param poolOffset Unused (ITEM_GIVE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneItemGiveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -9,13 +9,13 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
cutscenejsonfield.c
cutscenejsonentityindex.c
cutscenejsonareaid.c
cutscenejsonpool.c
cutscenejsonpauseflags.c
cutscenejsoneasing.c
cutscenejsonemoji.c
cutscenejsonentitytype.c
cutscenejsonentitydir.c
cutscenejsonbattleencounter.c
cutscenejsonbattleability.c
cutscenejsonbattlestate.c
cutscenejsonscenetype.c
cutscenejsonuiscreentype.c
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "cutscenejsonbattleability.h"
#include "cutscenejsonenum.h"
static const cutscenejsonenumentry_t CUTSCENE_JSON_BATTLE_ABILITY_TABLE[] = {
{ "FIRE", BATTLE_ABILITY_FIRE },
};
#define CUTSCENE_JSON_BATTLE_ABILITY_TABLE_COUNT \
(sizeof(CUTSCENE_JSON_BATTLE_ABILITY_TABLE) / \
sizeof(cutscenejsonenumentry_t))
bool_t cutsceneJsonLookupBattleAbility(
const char_t *name,
battleability_t *out
) {
int32_t value;
if(!cutsceneJsonLookupEnumInsensitive(
CUTSCENE_JSON_BATTLE_ABILITY_TABLE,
CUTSCENE_JSON_BATTLE_ABILITY_TABLE_COUNT, name, &value
)) {
return false;
}
*out = (battleability_t)value;
return true;
}
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#include "rpg/battle/battleability.h"
/**
* Looks up name (case-insensitively) against the battle ability table.
*
* @param name Name to search for.
* @param out Set to the matching ability on success.
* @return true if a matching entry was found, false otherwise.
*/
bool_t cutsceneJsonLookupBattleAbility(
const char_t *name,
battleability_t *out
);
@@ -12,7 +12,7 @@
/**
* Parses a JSON array of pause flag name strings, OR-ing each into out.
* Shared by the top-level "pause" field (assetCutsceneLoaderSync) and the
* Shared by the top-level "pause" field (cutsceneParseDoc) and the
* SET_PAUSE item's "flags" field - both authored as the same shape.
*
* @param arr JSON array of pause flag name strings.
@@ -1,12 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "cutscenejsonpool.h"
size_t cutsceneJsonPoolAlign(const size_t size) {
return (size + 3) & ~((size_t)3);
}
@@ -1,19 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
/**
* Rounds size up to the next multiple of 4 - every pool entry starts
* 4-byte aligned so worldpos_t/multi-byte reads out of it never trap on
* alignment-sensitive hardware (e.g. PSP's MIPS core).
*
* @param size Size to round up.
* @return size rounded up to the next multiple of 4.
*/
size_t cutsceneJsonPoolAlign(const size_t size);
@@ -8,6 +8,8 @@
#pragma once
#include "rpg/cutscene/item/cutsceneitembase.h"
#include "rpg/overworld/maparea.h"
#include "rpg/cutscene/item/maparea/cutscenemapareawait.h"
#include "rpg/cutscene/item/maparea/cutscenemaparearemove.h"
typedef struct {
worldpos_t min;
@@ -26,9 +26,7 @@ bool_t cutsceneMapAreaRemoveUpdate(
errorret_t cutsceneMapAreaRemoveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
if(!cutsceneJsonGetAreaId(
yyjson_obj_get(itemObj, "areaId"), &item->mapAreaRemove.areaId
@@ -41,13 +41,9 @@ bool_t cutsceneMapAreaRemoveUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (MAP_AREA_REMOVE needs no pool storage).
* @param poolOffset Unused (MAP_AREA_REMOVE needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneMapAreaRemoveLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -10,7 +10,6 @@
#include "rpg/overworld/maparea.h"
#include "assert/assert.h"
#include "rpg/cutscene/item/json/cutscenejsonareaid.h"
#include "rpg/cutscene/item/json/cutscenejsonpool.h"
void cutsceneMapAreaWaitStart(
const cutsceneitem_t *item,
@@ -43,9 +42,7 @@ bool_t cutsceneMapAreaWaitUpdate(
errorret_t cutsceneMapAreaWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
yyjson_val *areaIds = yyjson_obj_get(itemObj, "areaIds");
size_t count = yyjson_arr_size(areaIds);
@@ -58,28 +55,18 @@ errorret_t cutsceneMapAreaWaitLoad(
(uint32_t)CUTSCENE_MAP_AREA_WAIT_MAX
);
}
*poolOffset = cutsceneJsonPoolAlign(*poolOffset);
uint8_t *dest = pool + *poolOffset;
yyjson_arr_iter iter = yyjson_arr_iter_with(areaIds);
yyjson_val *idVal;
size_t i = 0;
while((idVal = yyjson_arr_iter_next(&iter)) != NULL) {
if(!cutsceneJsonGetAreaId(idVal, &dest[i])) {
if(!cutsceneJsonGetAreaId(idVal, &item->mapAreaWait.areaIds[i])) {
errorThrow(
"Cutscene item (MAP_AREA_WAIT): areaIds[%u] is invalid", (uint32_t)i
);
}
i++;
}
item->mapAreaWait.areaIds = dest;
item->mapAreaWait.count = (uint8_t)count;
*poolOffset += count;
errorOk();
}
size_t cutsceneMapAreaWaitPoolSize(yyjson_val *itemObj) {
yyjson_val *areaIds = yyjson_obj_get(itemObj, "areaIds");
if(!yyjson_is_arr(areaIds)) return 0;
return yyjson_arr_size(areaIds);
}
@@ -12,7 +12,7 @@
#define CUTSCENE_MAP_AREA_WAIT_MAX 4
typedef struct {
const uint8_t *areaIds;
uint8_t areaIds[CUTSCENE_MAP_AREA_WAIT_MAX];
uint8_t count;
} cutscenemapareawait_t;
@@ -47,26 +47,13 @@ bool_t cutsceneMapAreaWaitUpdate(
/**
* Parses a MAP_AREA_WAIT cutscene item's JSON fields into item, writing its
* "areaIds" array into pool at *poolOffset and advancing it.
* "areaIds" array directly into item->mapAreaWait.areaIds.
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Base pointer of the entry's pool allocation.
* @param poolOffset In/out cursor into pool.
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneMapAreaWaitLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
/**
* Computes how many pool bytes this MAP_AREA_WAIT item's "areaIds" array
* will need.
*
* @param itemObj The item's JSON object.
* @return Pool bytes required (0 if "areaIds" is missing/not an array).
*/
size_t cutsceneMapAreaWaitPoolSize(yyjson_val *itemObj);
@@ -34,9 +34,7 @@ bool_t cutsceneSaveDeviceCheckUpdate(
errorret_t cutsceneSaveDeviceCheckLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
const char_t *successMarker =
yyjson_get_str(yyjson_obj_get(itemObj, "successMarker"));
@@ -46,13 +46,9 @@ bool_t cutsceneSaveDeviceCheckUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (SAVE_DEVICE_CHECK needs no pool storage).
* @param poolOffset Unused (SAVE_DEVICE_CHECK needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneSaveDeviceCheckLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
@@ -32,9 +32,7 @@ bool_t cutsceneSaveLoadAllSlotsUpdate(
errorret_t cutsceneSaveLoadAllSlotsLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
) {
const char_t *successMarker =
yyjson_get_str(yyjson_obj_get(itemObj, "successMarker"));
@@ -45,13 +45,9 @@ bool_t cutsceneSaveLoadAllSlotsUpdate(
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (SAVE_LOAD_ALL_SLOTS needs no pool storage).
* @param poolOffset Unused (SAVE_LOAD_ALL_SLOTS needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneSaveLoadAllSlotsLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
cutsceneitem_t *item
);
Loaded 100 of 160 files, more files were not shown because too many files have changed in this diff. Show more