Skip to Content
The Alchemy Project

src/field/field_engine_encounter.c

Random encounters, and each scene's music, area name and battle background. More…

Included by field_engine.c.

Macros

NameDescription
#defineWORLD_MAP_ENCOUNTER_REGIONS 20The World Map's encounter regions: a block's region number past them takes the encounters of its number modulo WORLD_MAP_ENCOUNTER_REGIONS (Prox's blizzard, 36, those of 16), the higher numbers choosing only the ground's battle environment (fdWorldMapGetBattleEnvironment).
#defineBATTLE_BACKGROUND_RULE 0x80000000A battle background rule's word (battle_background_rules.c's BATTLE_BACKGROUND): bit 31 marks a battle background, its resource id below; any other word is a scene.
#defineBATTLE_BACKGROUND_RULE_RESOURCE 0xffff

Functions

NameDescription
voidfdWorldMapMarkPlaceVisited (Sint32 id, Sint32 variant)Marks the world map place the party has just entered as visited: sets FLAG_PLACE_VISITED of the first place whose visitScene is id and whose visitEntrance is variant (or any, for -1).
Sint32fdFlagEntriesGetValue (Sint32 flag)Returns the markers of the World Map place numbered flag, or 1 when there is none or its markers are 0.
voidfdEncounterSelectMapZones (Sint32 useRules)Chooses the current scene's encounter zones, one for each kind of ground the party can stand on (FieldStateWork->encounterZones, read by fdEncounterRollMapStep), from the first rule matching the scene and entrance.
Sint32fdEncounterResolveWorldMapZoneAndBackdrop (void *pos)Returns the World Map encounter zone for the ground at position (0 for none), and chooses the battle background for its battle environment (MainRoutineStateWork.battleBg).
Sint32fdEncounterCountStepsAndSelectFormation (Sint32 zone, FIXED steps)Advances the encounter counter for zone and returns the formation to fight, or 0 for no battle. steps (16.16) scales how far the counter moves; a battle starts once it reaches encounterThreshold.
Sint32fdEncounterRollMapStep (Uint32 terrain, FIXED steps)Rolls a random battle on the encounter zone of the ground underfoot. terrain: the ground, an index into the field state's encounterZones: 0 for ordinary ground, 1 for a MAP_METATILE_SECOND_ZONE metatile. steps: the step weight, passed on to fdEncounterCountStepsAndSelectFormation.
Sint32fdEncounterRollWorldMap (Sint32 pos, FIXED steps)Rolls a random battle on the World Map, taking the zone from the metatile at pos.
Uint16fdEncounterGetFormationAtSlot (Sint32 zone, Sint32 slot)Returns the formation in slot (0 to 7) of zone.
Uint16fdEncounterGetDjinnFormation (Sint32 djinni)Returns the formation of Djinni battle djinni, which the table gives as a zone and slot.
voidfdChooseMusic (void)Chooses the current scene's field music (MainRoutineStateWork.sound) from the first rule matching the scene or its area and the entrance; SOUND_KEEP_BGM when none does.
Sint32fdAreaGetNameIndex (Sint32 sceneId, Sint32 entrance)Returns the area name of sceneId and entrance, from the first rule matching the scene or its area; 0, the Title's name, when none does.
voidfdSetRestartPoint (void)Sets the scene the party returns to after a defeat (MainRoutineStateWork.restartScene and restartEntrance), from the first rule of SceneReturnRules that matches the current scene and entrance: rows of eight halfwords that open {scene, entrance (-1 for any), return scene, return entrance}, ended by scene -1.
voidfdWorldMapSetReturnForScene (Sint32 scene, Sint32 entrance)Where the party comes out on the world map after reaching scene and entrance (Teleport's choice calls it).
voidfdWorldMapSetReturnLeavingScene (Sint32 scene, Sint32 entrance)fdWorldMapSetReturnForScene's twin for the rule's return scene and entrance (its third and fourth values): with FLAG_SHIP_CAN_SAIL set, clears ownerOverride and copies the rule's position to the party's saved position.
Sint16fdBattleBackgroundGetSelected (void)Returns the battle background chosen for the current scene.
voidfdBattleBackgroundSelectForMap (void)Chooses the battle background for the current scene.
voidfdBattleBackgroundSelectForEnvironment (Sint32 environment)Chooses the battle background for a World Map battle environment (enum BATTLE_ENVIRONMENT; any other gives the ship deck's) and sets MainRoutineStateWork.battleBg.
BoolfdPartyIsInFrontRow (Sint32 owner)Whether the character is one of the first PARTY_BATTLE_SIZE party members, the ones in battle.
voidfdEncounterSelectBattleMusic (Sint32 zone, Sint32 slot)Chooses the battle music for slot of zone (MainRoutineStateWork.battleMusic): a key takes the song listed last before it in EncounterBattleMusic, and a link battle (FLAG_LINK_BATTLE) keeps the music playing.

Description

Random encounters, and each scene's music, area name and battle background. Part of field_engine.c.

Macros in detail

WORLD_MAP_ENCOUNTER_REGIONS

#define WORLD_MAP_ENCOUNTER_REGIONS 20

The World Map's encounter regions: a block's region number past them takes the encounters of its number modulo WORLD_MAP_ENCOUNTER_REGIONS (Prox's blizzard, 36, those of 16), the higher numbers choosing only the ground's battle environment (fdWorldMapGetBattleEnvironment).

Referenced by fdEncounterResolveWorldMapZoneAndBackdrop().

Definition at line 85 of file field_engine_encounter.c.

BATTLE_BACKGROUND_RULE

#define BATTLE_BACKGROUND_RULE 0x80000000

A battle background rule's word (battle_background_rules.c's BATTLE_BACKGROUND): bit 31 marks a battle background, its resource id below; any other word is a scene.

Referenced by fdBattleBackgroundSelectForMap().

Definition at line 410 of file field_engine_encounter.c.

BATTLE_BACKGROUND_RULE_RESOURCE

#define BATTLE_BACKGROUND_RULE_RESOURCE 0xffff

Referenced by fdBattleBackgroundSelectForMap().

Definition at line 411 of file field_engine_encounter.c.

Functions in detail

fdWorldMapMarkPlaceVisited

void fdWorldMapMarkPlaceVisited(Sint32 id, Sint32 variant)

Marks the world map place the party has just entered as visited: sets FLAG_PLACE_VISITED of the first place whose visitScene is id and whose visitEntrance is variant (or any, for -1). The main routine calls it at every scene entry; fdMapScreenShow offers the marked places.

References fdWorldMapPlacesGetTable(), Sint32 and syGameFlagSet().

Referenced by gmArrive().

Definition at line 12 of file field_engine_encounter.c.

fdFlagEntriesGetValue

Sint32 fdFlagEntriesGetValue(Sint32 flag)

Returns the markers of the World Map place numbered flag, or 1 when there is none or its markers are 0.

References fdWorldMapPlacesGetTable() and Sint32.

Referenced by fdEngineRun().

Definition at line 29 of file field_engine_encounter.c.

fdEncounterSelectMapZones

void fdEncounterSelectMapZones(Sint32 useRules)

Chooses the current scene's encounter zones, one for each kind of ground the party can stand on (FieldStateWork->encounterZones, read by fdEncounterRollMapStep), from the first rule matching the scene and entrance.

References fdBattleBackgroundSelectForMap(), MainRoutineStateWork, SceneEncounterRules, Sint32 and syGameFlagGet().

Referenced by fdEngineRun().

Definition at line 44 of file field_engine_encounter.c.

fdEncounterResolveWorldMapZoneAndBackdrop

Sint32 fdEncounterResolveWorldMapZoneAndBackdrop(void *pos)

Returns the World Map encounter zone for the ground at position (0 for none), and chooses the battle background for its battle environment (MainRoutineStateWork.battleBg). Flying, there are none; aboard the ship, the zones are WorldMapShipEncounterRules'; a rule's flag must be set for it to apply.

References fdBattleBackgroundSelectForEnvironment(), fdWorldMapGetBattleEnvironmentStub(), MainRoutineStateWork, Sint32, syGameFlagGet(), WorldMapEncounterRules and WorldMapShipEncounterRules.

Referenced by fdEncounterRollWorldMap(), fdRequestBattleFromEncounterTable() and fdRequestDjinnBattle().

Definition at line 92 of file field_engine_encounter.c.

fdEncounterCountStepsAndSelectFormation

Sint32 fdEncounterCountStepsAndSelectFormation(Sint32 zone, FIXED steps)

Advances the encounter counter for zone and returns the formation to fight, or 0 for no battle. steps (16.16) scales how far the counter moves; a battle starts once it reaches encounterThreshold.

References EncounterZones, fdEncounterSelectBattleMusic(), gmPartyGetAverageLevelStub(), MainRoutineStateWork, Sint32, syDivFX(), syGameFlagGet() and syRandom().

Referenced by fdEncounterRollMapStep() and fdEncounterRollWorldMap().

Definition at line 125 of file field_engine_encounter.c.

fdEncounterRollMapStep

Sint32 fdEncounterRollMapStep(Uint32 terrain, FIXED steps)

Rolls a random battle on the encounter zone of the ground underfoot. terrain: the ground, an index into the field state's encounterZones: 0 for ordinary ground, 1 for a MAP_METATILE_SECOND_ZONE metatile. steps: the step weight, passed on to fdEncounterCountStepsAndSelectFormation.

References fdEncounterCountStepsAndSelectFormation() and Sint32.

Referenced by fdUpdateWalking().

Definition at line 214 of file field_engine_encounter.c.

fdEncounterRollWorldMap

Sint32 fdEncounterRollWorldMap(Sint32 pos, FIXED steps)

Rolls a random battle on the World Map, taking the zone from the metatile at pos. Returns the formation to fight, or 0 for no battle.

References fdEncounterCountStepsAndSelectFormation(), fdEncounterResolveWorldMapZoneAndBackdrop() and Sint32.

Referenced by fdUpdateWalking().

Definition at line 223 of file field_engine_encounter.c.

fdEncounterGetFormationAtSlot

Uint16 fdEncounterGetFormationAtSlot(Sint32 zone, Sint32 slot)

Returns the formation in slot (0 to 7) of zone.

References EncounterZones and Sint32.

Referenced by fdEncounterGetDjinnFormation(), fdRequestBattleFromEncounterTable() and fdTalkToActor().

Definition at line 230 of file field_engine_encounter.c.

fdEncounterGetDjinnFormation

Uint16 fdEncounterGetDjinnFormation(Sint32 djinni)

Returns the formation of Djinni battle djinni, which the table gives as a zone and slot.

References EncounterDjinnBattles, fdEncounterGetFormationAtSlot() and Sint32.

Referenced by fdRequestDjinnBattle().

Definition at line 237 of file field_engine_encounter.c.

fdChooseMusic

void fdChooseMusic(void)

Chooses the current scene's field music (MainRoutineStateWork.sound) from the first rule matching the scene or its area and the entrance; SOUND_KEEP_BGM when none does.

References MainRoutineStateWork, SceneMusicRules and syGameFlagGet().

Referenced by gmMainRoutineRun(), scWorldMapBoardShip() and scWorldMapLeaveShip().

Definition at line 253 of file field_engine_encounter.c.

fdAreaGetNameIndex

Sint32 fdAreaGetNameIndex(Sint32 sceneId, Sint32 entrance)

Returns the area name of sceneId and entrance, from the first rule matching the scene or its area; 0, the Title's name, when none does.

References AreaNameRules, gmGetArea() and Sint32.

Referenced by fdMapScreenUpdateMarkers().

Definition at line 292 of file field_engine_encounter.c.

fdSetRestartPoint

void fdSetRestartPoint(void)

Sets the scene the party returns to after a defeat (MainRoutineStateWork.restartScene and restartEntrance), from the first rule of SceneReturnRules that matches the current scene and entrance: rows of eight halfwords that open {scene, entrance (-1 for any), return scene, return entrance}, ended by scene -1. Without a match the restart point stays as it was.

References MainRoutineStateWork and SceneReturnRules.

Referenced by fdEngineRun().

Definition at line 332 of file field_engine_encounter.c.

fdWorldMapSetReturnForScene

void fdWorldMapSetReturnForScene(Sint32 scene, Sint32 entrance)

Where the party comes out on the world map after reaching scene and entrance (Teleport's choice calls it). With FLAG_SHIP_CAN_SAIL set (the party has the ship, whose mooring the map screen marks), finds the first rule of SceneReturnRules for scene and entrance (-1 on either side matches any entrance) and copies its position, x and z in 16.16 and the third value as is, to MainRoutineStateWork.shipX, shipZ and shipFacing.

References MainRoutineStateWork, SceneReturnRules, Sint32 and syGameFlagGet().

Referenced by fdMapScreenShow().

Definition at line 359 of file field_engine_encounter.c.

fdWorldMapSetReturnLeavingScene

void fdWorldMapSetReturnLeavingScene(Sint32 scene, Sint32 entrance)

fdWorldMapSetReturnForScene's twin for the rule's return scene and entrance (its third and fourth values): with FLAG_SHIP_CAN_SAIL set, clears ownerOverride and copies the rule's position to the party's saved position.

References MainRoutineStateWork, SceneReturnRules, Sint32 and syGameFlagGet().

Referenced by fdEngineRun(), gmReturnFromEngine(), scInit() and scShamanVillageCaveLoseToGladiators().

Definition at line 381 of file field_engine_encounter.c.

fdBattleBackgroundGetSelected

Sint16 fdBattleBackgroundGetSelected(void)

Returns the battle background chosen for the current scene.

References MainRoutineStateWork.

Definition at line 402 of file field_engine_encounter.c.

fdBattleBackgroundSelectForMap

void fdBattleBackgroundSelectForMap(void)

Chooses the battle background for the current scene. SceneBattleBackgroundRules lists each battle background (a word with bit 31 set) followed by the scenes that use it, ended by 0; a scene takes the one listed last before it, and a scene not listed takes the last one. Once the Mars Lighthouse has thawed, the second dragon hall backdrop becomes the first.

References MainRoutineStateWork, SceneBattleBackgroundRules, Sint32 and syGameFlagGet().

Referenced by fdEncounterSelectMapZones().

Definition at line 419 of file field_engine_encounter.c.

fdBattleBackgroundSelectForEnvironment

void fdBattleBackgroundSelectForEnvironment(Sint32 environment)

Chooses the battle background for a World Map battle environment (enum BATTLE_ENVIRONMENT; any other gives the ship deck's) and sets MainRoutineStateWork.battleBg.

References MainRoutineStateWork and Sint32.

Referenced by fdEncounterResolveWorldMapZoneAndBackdrop().

Definition at line 447 of file field_engine_encounter.c.

fdPartyIsInFrontRow

Bool fdPartyIsInFrontRow(Sint32 owner)

Whether the character is one of the first PARTY_BATTLE_SIZE party members, the ones in battle.

References gmPartyCountMembersStub(), MainRoutineStateWork and Sint32.

Referenced by fdEncounterSelectBattleMusic().

Definition at line 481 of file field_engine_encounter.c.

fdEncounterSelectBattleMusic

void fdEncounterSelectBattleMusic(Sint32 zone, Sint32 slot)

Chooses the battle music for slot of zone (MainRoutineStateWork.battleMusic): a key takes the song listed last before it in EncounterBattleMusic, and a link battle (FLAG_LINK_BATTLE) keeps the music playing. With no song found, it is MUS_BATTLE_ON_THE_SEA on the ship before it flies, else by the front row: MUS_BATTLE_THE_LOST_AGE with Felix, MUS_BATTLE_THE_BROKEN_SEAL with Isaac, MUS_BATTLE_JENNA with Jenna, or MUS_BATTLE_THE_LOST_AGE.

References EncounterBattleMusic, fdPartyIsInFrontRow(), MainRoutineStateWork, Sint32 and syGameFlagGet().

Referenced by fdEncounterCountStepsAndSelectFormation(), fdRequestBattleFromEncounterTable(), fdRequestDjinnBattle() and fdTalkToActor().

Definition at line 502 of file field_engine_encounter.c.