28#define UNIT_STAT_DIFF(a, b) ((a) - (b) < 0 ? (b) - (a) : (a) - (b))
62 for (partyIndex = 0; partyIndex < partySize; partyIndex++) {
65 totalLevels = totalLevels / partySize;
78 void *caller = __builtin_return_address(0);
83 asm(
"" : :
"l"(caller));
87 unit = &states[owner];
106 recordIndex = selector - 8;
110 if (recordIndex > 164U) {
112 if (recordIndex > 0xF9U) {
125 if (direction != 0) {
129 *second++ = *first++;
131 }
while (index < count);
133 }
else if (count > 0) {
136 *first++ = *second++;
138 }
while (index != 0);
152 s32 index, effectIndex, elementIndex;
211 for (effectIndex = 0; effectIndex < 4; effectIndex++) {
214 stats->
kind = effectKind;
215 stats->
amount = effectAmount;
216 switch (stats->
kind) {
280 for (elementIndex = 0; elementIndex <
NUM_ELEMENTS; elementIndex++) {
283 for (index = 0; index < 20; index++) {
284 if (activeDjinn & (1 << index)) {
288 stats->
hp += djinn->
hp;
289 stats->
pp += djinn->
pp;
302 stats->
hp = stats->
hp * classDefinition->
hp / 10;
303 stats->
pp = stats->
pp * classDefinition->
pp / 10;
304 stats->
atk = stats->
atk * classDefinition->
atk / 10;
305 stats->
def = stats->
def * classDefinition->
def / 10;
306 stats->
agi = stats->
agi * classDefinition->
agi / 10;
314 for (effectIndex = 0; effectIndex < 4; effectIndex++) {
317 stats->
kind = effectKind;
319 stats->
amount = effectAmount;
320 switch (effectKind) {
417 previousMaximum = unit->
maxHp;
418 CLAMP(stats->
hp, 0, statLimit);
420 if (previousMaximum != unit->
maxHp) {
421 value = stats->
hp * unit->
hpGauge / 0x4000;
422 CLAMP(value, 0, statLimit);
423 if (unit->
hp != 0 && value == 0)
428 previousMaximum = unit->
maxPp;
429 CLAMP(stats->
pp, 0, statLimit);
431 if (previousMaximum != unit->
maxPp) {
432 value = stats->
pp * unit->
ppGauge / 0x4000;
433 CLAMP(value, 0, statLimit);
434 if (unit->
pp != 0 && value == 0)
452 for (partyIndex = 0; partyIndex < partySize; partyIndex++) {
457 for (inventorySlot = 0; inventorySlot <
INVENTORY_SLOTS; inventorySlot++) {
463 for (effectIndex = 0; effectIndex < 4; effectIndex++) {
466 effectKind = effect->
kind;
500 hours = (hourTens -
'0') * 10;
504 hours += hourUnits -
'0';
505 minuteTens = *digits;
506 minutes = (minuteTens -
'0') * 10;
508 minuteUnits = *digits;
510 minutes += minuteUnits -
'0';
511 secondTens = digits[0];
512 seconds = (secondTens -
'0') * 10;
513 secondUnits = digits[1];
514 seconds += secondUnits -
'0';
515 packed = (((hours << 4) + minutes) << 6) + seconds;
516 shifted = 0x80 << 21;
517 shifted |= packed << 16;
518 result = shifted >> 16;
520 result |= 0xffff8000;
630 for (owner = 0; owner <= 1; owner++) {
668 for (partyIndex = 0; partyIndex < partySize; partyIndex++) {
675 if (cureConditions == 1) {
694 for (partyIndex = 0; partyIndex < partySize; partyIndex++) {
698 if (elementGroup == 0) {
710 if (restorePp == 0) {
772 s32 currentHp = unit->
hp;
774 s32 value = currentHp + delta;
789 s32 currentPp = unit->
pp;
791 s32 value = currentPp + delta;
#define Unit_UpdateGauges(unit)
Recalculates HP and PP gauges, keeping at least one when any points remain.
void Game_ResetForNewGameFar(s32 mode)
s32 GameFlag_Test(s32 flag)
Returns 1 when flag is set.
s32 GameFlag_SetBit(s32 flag)
Sets flag and returns its byte number.
void GameFlag_ClearBit(s32 flag)
Clears flag.
#define FLAG_MERCURY_LIGHTHOUSE_LIT
#define FLAG_VENUS_LIGHTHOUSE_LIT
#define FLAG_KEEP_MUSIC
The next scene keeps the music that is playing; scene.c clears FLAG_KEEP_MUSIC as it does,...
#define FLAG_MARS_LIGHTHOUSE_LIT
#define FLAG_JUPITER_LIGHTHOUSE_LIT
#define FLAG_CHILDHOOD
the prologue: Isaac and Garet are children
#define FLAG_KEEP_MUSIC_HELD
#define FLAG_LURE_CAP
Set while the Lure Cap is worn: random encounters come twice as fast.
#define FLAG_ISAAC_WEARS_TUNIC
The young Isaac wears the tunic Dora made him take on the stormy night: placed child sprites get its ...
u32 gPlayTimeFrames
The play time clock, in frames.
volatile u16 gIdleFrames
Frames since a key was last held.
u8 gDebugMode
Debug builds: Start+Select pauses the frame loop.
volatile u8 gIdleSleepEnabled
Sleep after three idle minutes.
void Unit_UpdateClass(s32 ownerId)
Works out a character's class again after its Djinn change, then its Psynergy list and its base eleme...
struct ClassDefinition * Class_GetDefinition(s32 classId)
Returns class classId's definition: its series, element requirements, stat multipliers and the Psyner...
const struct DjinnDefinition * Djinn_GetDefinition(u32 group, u32 index)
Returns the definition of Djinn index (0 to 19) of element group (0 to 3); anything out of range give...
const struct EnemyDefinition gEnemy_Definitions[]
const u8 gCharacter_ElementGroupTable[]
What every part of the game object shares: its includes and the inventory helper two parts use.
struct GameState gGameState
void * Runtime_BumpAllocateAlternatePool(s32 size)
As MemAlloc, but tries EWRAM first.
s32 Inventory_Equip(s32 owner, s32 slot)
Equips the item in slot, clearing the equipped bit of the item of the same type it replaces.
struct ItemDefinition * Item_GetDirect(s32 item)
Returns item item's definition; an inventory entry may be passed, its equipped and quantity bits are ...
@ ITEM_EFFECT_ADD_MARS_RESIST
@ ITEM_EFFECT_24
summed by UnitCalc; TBS never reads it
@ ITEM_EFFECT_ADD_JUPITER_POWER
@ ITEM_EFFECT_ADD_VENUS_RESIST
@ ITEM_EFFECT_LURE
sets FLAG_LURE_CAP while worn: encounters come faster
@ ITEM_EFFECT_ADD_AGILITY
@ ITEM_EFFECT_ADD_HP_REGEN
@ ITEM_EFFECT_ADD_JUPITER_RESIST
@ ITEM_EFFECT_ADD_PP_REGEN
@ ITEM_EFFECT_25
sets restraint bit 3, which lifts bit 0
@ ITEM_EFFECT_ADD_VENUS_POWER
Element power and resistance, in the element order Venus, Mercury, Mars, Jupiter.
@ ITEM_EFFECT_ADD_MERCURY_POWER
@ ITEM_EFFECT_ADD_TURNS
more actions each round
@ ITEM_EFFECT_ADD_UNLEASH_CHANCE
percent added to the weapon's unleash
@ ITEM_EFFECT_ADD_MARS_POWER
@ ITEM_EFFECT_ADD_MERCURY_RESIST
s32 Unit_LearnPsynergy(s32 stateIndex, s32 value)
Teaches a character action value for good (or makes a class action permanent by clearing its marks),...
void Party_InitCharacters(void)
Sets up all eight characters for a new game: names from the message table, starting equipment equippe...
#define CLAMP(v, low, high)
Keeps v within low to high, the low bound tested first.
void MemFree(void *buffer)
s32 Party_CountActiveOwners(void)
Returns the number of party members, counted from game flags 0 to 7.
s32 Party_AddActiveOwner(s32 owner)
Adds character owner to the end of the party: sets its game flag and appends it to gGameState....
char ResourceId_BuildStamp
#define START_DMA(source_, destination_, control_)
s16 resistance
Elemental resistance to it.
s16 power
Elemental power in the element.
A unit in battle: a party member's or an enemy's stats, Psynergy, items, Djinn and ailments.
u8 luck
0 to 99 (UnitCalc)
u16 baseDefense
before equipment, Djinn, class and stages
u16 baseAttack
before equipment, Djinn, class and stages
u16 baseAgility
before equipment, Djinn, class and stages
u8 hpRegen
HP regained at each turn's end (UnitCalc).
s8 elementLevels[4]
A summon's boost to each element: the Djinn of that element the summon spent, adding (n * n + n) * 5 ...
u32 djinnActive[4]
The Djinn set, one bit per Djinni; only these add their stat bonuses.
u8 ppRegen
PP regained at each turn's end, 0 to 200 (UnitCalc): 4 more once the lighthouse of the character's el...
s16 maxPp
0 to 1999 for a character, 9999 for an enemy (UnitCalc)
s16 hpGauge
hp / maxHp in 0..0x4000; 1 while any HP remains
s8 poison
1 poisoned, 2 deadly poison
u8 haunt
set while an evil spirit grips the unit
s16 hp
current HP, 0 when downed
s16 ppGauge
pp / maxPp in 0..0x4000; 1 while any PP remains
struct BattleElementStats element[4]
Power and resistance in each element, 0 to 200, after equipment, element levels and Resistance stages...
u8 baseActionCount
Actions a round before equipment: the low four bits are read.
u8 baseHpRegen
HP regained each turn before equipment; 0 for a character.
u8 baseLuck
before equipment, Djinn and class
u8 classId
Who the unit is: a character's id (ISAAC to JENNA, below 8) or an enemy's id (BattleUnitSet).
struct BattleElementStats baseElement[4]
Power and resistance before equipment and stages: an enemy's from its element preset,...
s16 basePp
before equipment, Djinn and class (UnitCalc)
s16 maxHp
0 to 1999 for a character, 9999 for an enemy (UnitCalc)
s8 agilityStage
The Agility stage: -4 (Vine) to 8 (Zephyr, twice the Agility).
u8 basePpRegen
PP regained each turn before equipment and lighthouses.
u8 classIndex
The character's class, worked out from its Djinn (Unit_UpdateClass); 0 marks an enemy,...
s16 baseHp
before equipment, Djinn and class (UnitCalc)
u16 atk
Attack, 0 to 999 (UnitCalc).
u8 equipBonus[2]
The totals of equipment effects 23 (unleash chance) and 24 that UnitCalc sums; TBS never reads them b...
u8 level
1 to 99 for a character
u16 agi
Agility, 0 to 999: the order of the round's actions.
u16 def
Defense, 0 to 999 (UnitCalc).
u16 inventory[15]
Items held, one entry each: bits 15..11 quantity | 10 broken | 9 equipped | 8..0 item id (INVENTORY_*...
u8 ready
Kite's extra action: 2 when cast, down one at each turn's end; while nonzero UnitCalc gives the unit ...
u8 actionEntryCount
Actions each round, 0 to 2: baseActionCount, one more while ready, and the equipment's extra turns (U...
A class: its series, the element levels it needs, its stat multipliers and the abilities it teaches.
u8 pp
tenths of the base stat
u8 def
tenths of the base stat
u8 atk
tenths of the base stat
u8 luck
tenths of the base stat
u8 agi
tenths of the base stat
u8 hp
tenths of the base stat
A Djinni: its ability and the stat bonuses it gives while set.
s8 hp
Added to the base stats while the Djinni is set (UnitCalc), before the class multiplies them.
u16 firstShortcut
The shortcuts' assignments.
u8 unk20b
1 on a new game; not read by name in TBS
u16 bKeys
KEY_B; not read by name in TBS.
u8 paletteGlow[2]
window hue (0 to 24), brightness
u32 playTime
frames, copied from gPlayTimeFrames when saving
u32 buildStamp
Runtime_GetBuildStampTime's value when the game was last saved; its low half goes into the file's sum...
u16 selectKeys
the menu; KEY_SELECT
u8 idleSleepEnabled
the option, saved from gIdleSleepEnabled
s8 prizeWeights[0x0f]
The slot machine's prize weights, three prizes to each of 4, 8, 16, 32 and 64 on a new game; not read...
struct BattleUnit owners[8]
the characters
u8 unk008[8]
Not named by TBS code.
u8 activeOwners[8]
the party in order
u8 messageSpeed
0 to 2; 1 on a new game
u16 secondShortcutKeys
secondShortcut; KEY_R
u32 startStamp
Runtime_GetBuildStampTime's value at a new game.
s32 selectedActor
the leader: its unit and its actor
u16 aKeys
The buttons for each field command, as KEY_ masks; read with gKeysPressed except dashKeys,...
u8 speech
1 shows speakers' portraits beside their messages
u16 firstShortcutKeys
firstShortcut; KEY_L
An item's definition: its price, type, who can equip it, its bonuses and unleash, its effects and the...
s8 secondaryBonus
Defense added while equipped.
s16 primaryBonus
Attack added while equipped.
struct ItemEffect effects[4]
what it does while equipped
One effect of an item while equipped: its kind (enum ItemEffectKind) and amount.
u8 kind
enum ItemEffectKind; 0 for none
s8 amount
what the kind adds or scales by
The end of the scene state that a new game sets up.
The running totals UnitCalc builds a unit's stats in, in a block of the alternate pool that it frees ...
s32 unk4c[2]
never written
s32 amount
the amount of the item effect being applied
s32 kind
the item effect being applied
s32 unk14
never written; a gap between Agility and Luck
s32 element[4][2]
[element][0] power and [1] resistance
s32 ppRegen
PP regained each round.
struct ItemDefinition * item
the equipped item being applied
s32 hpRegen
HP regained each round.
s32 turns
actions a round, 0 to 2
void * Resource_GetTableEntry(s32 index)
Returns the ROM address of resource index from the resource directory.
void UnitCalc(s32 owner)
Recomputes a unit's stats from its base stats, equipment, set Djinn, class and battle stat levels,...
const u8 * Enemy_GetDefinition(s32 selector)
Returns the definition of enemy selector - 8; an id past the first 250 gives the first.
void Party_RestorePpOfLitElements(void)
Restores the PP of each party member whose element group has a lit lighthouse: Venus or Mars for grou...
static s32 Unit_ClampGaugeValue(s32 value, s32 maximum)
Clamps a new HP or PP value to 0 and the maximum.
s16 Unit_AddPp(s32 owner, s32 delta)
Adds delta to the PP of unit owner, clamped to 0..maxPp, recomputes its gauges and returns the new PP...
#define UNIT_STAT_DIFF(a, b)
void Party_RestoreHpPp(s32 cureConditions)
Restores every party member's HP and PP; with cureConditions 1 it also cures poison and haunting.
void Unit_SetPp(struct BattleUnit *unit, s32 pp)
Sets the unit's PP, clamped to 0..maxPp, and recomputes its HP and PP gauges.
void Runtime_CopyBytesDirectional(u8 *first, u8 *second, s32 count, s32 direction)
Copies count bytes from first to second when direction is set, and from second to first otherwise.
u32 Party_GetAverageLevel(void)
Returns the average level of the party members, rounded down (0 with no members).
struct BattleUnit * UnitGet(u32 owner)
Returns the unit record of owner: 0 to 7 are the playable characters in the game state,...
void * Djinn_GetRecoveryTable(s32 enemySide)
Returns the party's Djinn recovery bank (the standby Djinn and their turns to recover),...
void GameFlag_RefreshLureCap(void)
Sets FLAG_LURE_CAP when any party member has the Lure Cap (the one item with ITEM_EFFECT_LURE) equipp...
void UnitRatio(s32 owner)
Recomputes the HP and PP gauges of owner from its current HP and PP, so that UnitCalc keeps the same ...
UNUSED void Owner_RatioNoOp(void)
Unused reserved entry; keeps its place in the far-call table.
s32 Unit_AddHp(s32 owner, s32 delta)
Adds delta to the HP of unit owner, clamped to 0..maxHp, recomputes its gauges and returns the new HP...
void Party_BeginChildhood(void)
Sets FLAG_CHILDHOOD and recomputes Isaac, Garet and Jenna, whose classes then take their childhood fo...
void GameState_InitDefaults(void)
Clears the game-state header, scene state, flags and save stamp; sets a new game's defaults: Isaac al...
void Game_Restart(void)
Starts a new game: clears the game state and restarts at the opening.
void Unit_SetHp(struct BattleUnit *unit, s32 hp)
Sets the unit's HP, clamped to 0..maxHp, and recomputes its HP and PP gauges.
u32 Runtime_GetBuildStampTime(void)
Encodes the build-stamp time as ((hours * 16 + minutes) * 64 + seconds) with bit 12 set.
void Party_EndChildhood(void)
The party set-up after the prologue: flags, Jenna recomputed, Isaac and Garet healed with the Machete...