overworld.asm 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. HandleMidJump::
  2. ; Handle the player jumping down
  3. ; a ledge in the overworld.
  4. jpba _HandleMidJump
  5. EnterMap::
  6. ; Load a new map.
  7. ld a, $ff
  8. ld [wJoyIgnore], a
  9. call LoadMapData
  10. callba ClearVariablesOnEnterMap
  11. ld hl, wd72c
  12. bit 0, [hl] ; has the player already made 3 steps since the last battle?
  13. jr z, .skipGivingThreeStepsOfNoRandomBattles
  14. ld a, 3 ; minimum number of steps between battles
  15. ld [wNumberOfNoRandomBattleStepsLeft], a
  16. .skipGivingThreeStepsOfNoRandomBattles
  17. ld hl, wd72e
  18. bit 5, [hl] ; did a battle happen immediately before this?
  19. res 5, [hl] ; unset the "battle just happened" flag
  20. call z, ResetUsingStrengthOutOfBattleBit
  21. call nz, MapEntryAfterBattle
  22. ld hl, wd732
  23. ld a, [hl]
  24. and 1 << 4 | 1 << 3 ; fly warp or dungeon warp
  25. jr z, .didNotEnterUsingFlyWarpOrDungeonWarp
  26. res 3, [hl]
  27. callba EnterMapAnim
  28. call UpdateSprites
  29. .didNotEnterUsingFlyWarpOrDungeonWarp
  30. callba CheckForceBikeOrSurf ; handle currents in SF islands and forced bike riding in cycling road
  31. ld hl, wd72d
  32. res 5, [hl]
  33. call UpdateSprites
  34. ld hl, wCurrentMapScriptFlags
  35. set 5, [hl]
  36. set 6, [hl]
  37. xor a
  38. ld [wJoyIgnore], a
  39. OverworldLoop::
  40. call DelayFrame
  41. OverworldLoopLessDelay::
  42. call DelayFrame
  43. call LoadGBPal
  44. ld a, [wd736]
  45. bit 6, a ; jumping down a ledge?
  46. call nz, HandleMidJump
  47. ld a, [wWalkCounter]
  48. and a
  49. jp nz, .moveAhead ; if the player sprite has not yet completed the walking animation
  50. call JoypadOverworld ; get joypad state (which is possibly simulated)
  51. callba SafariZoneCheck
  52. ld a, [wSafariZoneGameOver]
  53. and a
  54. jp nz, WarpFound2
  55. ld hl, wd72d
  56. bit 3, [hl]
  57. res 3, [hl]
  58. jp nz, WarpFound2
  59. ld a, [wd732]
  60. and 1 << 4 | 1 << 3 ; fly warp or dungeon warp
  61. jp nz, HandleFlyWarpOrDungeonWarp
  62. ld a, [wCurOpponent]
  63. and a
  64. jp nz, .newBattle
  65. ld a, [wd730]
  66. bit 7, a ; are we simulating button presses?
  67. jr z, .notSimulating
  68. ld a, [hJoyHeld]
  69. jr .checkIfStartIsPressed
  70. .notSimulating
  71. ld a, [hJoyPressed]
  72. .checkIfStartIsPressed
  73. bit 3, a ; start button
  74. jr z, .startButtonNotPressed
  75. ; if START is pressed
  76. xor a
  77. ld [hSpriteIndexOrTextID], a ; start menu text ID
  78. jp .displayDialogue
  79. .startButtonNotPressed
  80. bit 0, a ; A button
  81. jp z, .checkIfDownButtonIsPressed
  82. ; if A is pressed
  83. ld a, [wd730]
  84. bit 2, a
  85. jp nz, .noDirectionButtonsPressed
  86. call IsPlayerCharacterBeingControlledByGame
  87. jr nz, .checkForOpponent
  88. call CheckForHiddenObjectOrBookshelfOrCardKeyDoor
  89. ld a, [$ffeb]
  90. and a
  91. jp z, OverworldLoop ; jump if a hidden object or bookshelf was found, but not if a card key door was found
  92. call IsSpriteOrSignInFrontOfPlayer
  93. ld a, [hSpriteIndexOrTextID]
  94. and a
  95. jp z, OverworldLoop
  96. .displayDialogue
  97. predef GetTileAndCoordsInFrontOfPlayer
  98. call UpdateSprites
  99. ld a, [wFlags_0xcd60]
  100. bit 2, a
  101. jr nz, .checkForOpponent
  102. bit 0, a
  103. jr nz, .checkForOpponent
  104. aCoord 8, 9
  105. ld [wTilePlayerStandingOn], a ; unused?
  106. call DisplayTextID ; display either the start menu or the NPC/sign text
  107. ld a, [wEnteringCableClub]
  108. and a
  109. jr z, .checkForOpponent
  110. dec a
  111. ld a, 0
  112. ld [wEnteringCableClub], a
  113. jr z, .changeMap
  114. ; XXX can this code be reached?
  115. predef LoadSAV
  116. ld a, [wCurMap]
  117. ld [wDestinationMap], a
  118. call SpecialWarpIn
  119. ld a, [wCurMap]
  120. call SwitchToMapRomBank ; switch to the ROM bank of the current map
  121. ld hl, wCurMapTileset
  122. set 7, [hl]
  123. .changeMap
  124. jp EnterMap
  125. .checkForOpponent
  126. ld a, [wCurOpponent]
  127. and a
  128. jp nz, .newBattle
  129. jp OverworldLoop
  130. .noDirectionButtonsPressed
  131. ld hl, wFlags_0xcd60
  132. res 2, [hl]
  133. call UpdateSprites
  134. ld a, 1
  135. ld [wCheckFor180DegreeTurn], a
  136. ld a, [wPlayerMovingDirection] ; the direction that was pressed last time
  137. and a
  138. jp z, OverworldLoop
  139. ; if a direction was pressed last time
  140. ld [wPlayerLastStopDirection], a ; save the last direction
  141. xor a
  142. ld [wPlayerMovingDirection], a ; zero the direction
  143. jp OverworldLoop
  144. .checkIfDownButtonIsPressed
  145. ld a, [hJoyHeld] ; current joypad state
  146. bit 7, a ; down button
  147. jr z, .checkIfUpButtonIsPressed
  148. ld a, 1
  149. ld [wSpriteStateData1 + 3], a ; delta Y
  150. ld a, PLAYER_DIR_DOWN
  151. jr .handleDirectionButtonPress
  152. .checkIfUpButtonIsPressed
  153. bit 6, a ; up button
  154. jr z, .checkIfLeftButtonIsPressed
  155. ld a, -1
  156. ld [wSpriteStateData1 + 3], a ; delta Y
  157. ld a, PLAYER_DIR_UP
  158. jr .handleDirectionButtonPress
  159. .checkIfLeftButtonIsPressed
  160. bit 5, a ; left button
  161. jr z, .checkIfRightButtonIsPressed
  162. ld a, -1
  163. ld [wSpriteStateData1 + 5], a ; delta X
  164. ld a, PLAYER_DIR_LEFT
  165. jr .handleDirectionButtonPress
  166. .checkIfRightButtonIsPressed
  167. bit 4, a ; right button
  168. jr z, .noDirectionButtonsPressed
  169. ld a, 1
  170. ld [wSpriteStateData1 + 5], a ; delta X
  171. .handleDirectionButtonPress
  172. ld [wPlayerDirection], a ; new direction
  173. ld a, [wd730]
  174. bit 7, a ; are we simulating button presses?
  175. jr nz, .noDirectionChange ; ignore direction changes if we are
  176. ld a, [wCheckFor180DegreeTurn]
  177. and a
  178. jr z, .noDirectionChange
  179. ld a, [wPlayerDirection] ; new direction
  180. ld b, a
  181. ld a, [wPlayerLastStopDirection] ; old direction
  182. cp b
  183. jr z, .noDirectionChange
  184. ; Check whether the player did a 180-degree turn.
  185. ; It appears that this code was supposed to show the player rotate by having
  186. ; the player's sprite face an intermediate direction before facing the opposite
  187. ; direction (instead of doing an instantaneous about-face), but the intermediate
  188. ; direction is only set for a short period of time. It is unlikely for it to
  189. ; ever be visible because DelayFrame is called at the start of OverworldLoop and
  190. ; normally not enough cycles would be executed between then and the time the
  191. ; direction is set for V-blank to occur while the direction is still set.
  192. swap a ; put old direction in upper half
  193. or b ; put new direction in lower half
  194. cp (PLAYER_DIR_DOWN << 4) | PLAYER_DIR_UP ; change dir from down to up
  195. jr nz, .notDownToUp
  196. ld a, PLAYER_DIR_LEFT
  197. ld [wPlayerMovingDirection], a
  198. jr .holdIntermediateDirectionLoop
  199. .notDownToUp
  200. cp (PLAYER_DIR_UP << 4) | PLAYER_DIR_DOWN ; change dir from up to down
  201. jr nz, .notUpToDown
  202. ld a, PLAYER_DIR_RIGHT
  203. ld [wPlayerMovingDirection], a
  204. jr .holdIntermediateDirectionLoop
  205. .notUpToDown
  206. cp (PLAYER_DIR_RIGHT << 4) | PLAYER_DIR_LEFT ; change dir from right to left
  207. jr nz, .notRightToLeft
  208. ld a, PLAYER_DIR_DOWN
  209. ld [wPlayerMovingDirection], a
  210. jr .holdIntermediateDirectionLoop
  211. .notRightToLeft
  212. cp (PLAYER_DIR_LEFT << 4) | PLAYER_DIR_RIGHT ; change dir from left to right
  213. jr nz, .holdIntermediateDirectionLoop
  214. ld a, PLAYER_DIR_UP
  215. ld [wPlayerMovingDirection], a
  216. .holdIntermediateDirectionLoop
  217. ld hl, wFlags_0xcd60
  218. set 2, [hl]
  219. ld hl, wCheckFor180DegreeTurn
  220. dec [hl]
  221. jr nz, .holdIntermediateDirectionLoop
  222. ld a, [wPlayerDirection]
  223. ld [wPlayerMovingDirection], a
  224. call NewBattle
  225. jp c, .battleOccurred
  226. jp OverworldLoop
  227. .noDirectionChange
  228. ld a, [wPlayerDirection] ; current direction
  229. ld [wPlayerMovingDirection], a ; save direction
  230. call UpdateSprites
  231. ld a, [wWalkBikeSurfState]
  232. cp $02 ; surfing
  233. jr z, .surfing
  234. ; not surfing
  235. call CollisionCheckOnLand
  236. jr nc, .noCollision
  237. ; collision occurred
  238. push hl
  239. ld hl, wd736
  240. bit 2, [hl] ; standing on warp flag
  241. pop hl
  242. jp z, OverworldLoop
  243. ; collision occurred while standing on a warp
  244. push hl
  245. call ExtraWarpCheck ; sets carry if there is a potential to warp
  246. pop hl
  247. jp c, CheckWarpsCollision
  248. jp OverworldLoop
  249. .surfing
  250. call CollisionCheckOnWater
  251. jp c, OverworldLoop
  252. .noCollision
  253. ld a, $08
  254. ld [wWalkCounter], a
  255. jr .moveAhead2
  256. .moveAhead
  257. ld a, [wd736]
  258. bit 7, a
  259. jr z, .noSpinning
  260. callba LoadSpinnerArrowTiles
  261. .noSpinning
  262. call UpdateSprites
  263. .moveAhead2
  264. ld hl, wFlags_0xcd60
  265. res 2, [hl]
  266. ld a, [wWalkBikeSurfState]
  267. dec a ; riding a bike?
  268. jr nz, .normalPlayerSpriteAdvancement
  269. ld a, [wd736]
  270. bit 6, a ; jumping a ledge?
  271. jr nz, .normalPlayerSpriteAdvancement
  272. call DoBikeSpeedup
  273. .normalPlayerSpriteAdvancement
  274. call AdvancePlayerSprite
  275. ld a, [wWalkCounter]
  276. and a
  277. jp nz, CheckMapConnections ; it seems like this check will never succeed (the other place where CheckMapConnections is run works)
  278. ; walking animation finished
  279. ld a, [wd730]
  280. bit 7, a
  281. jr nz, .doneStepCounting ; if button presses are being simulated, don't count steps
  282. ; step counting
  283. ld hl, wStepCounter
  284. dec [hl]
  285. ld a, [wd72c]
  286. bit 0, a
  287. jr z, .doneStepCounting
  288. ld hl, wNumberOfNoRandomBattleStepsLeft
  289. dec [hl]
  290. jr nz, .doneStepCounting
  291. ld hl, wd72c
  292. res 0, [hl] ; indicate that the player has stepped thrice since the last battle
  293. .doneStepCounting
  294. CheckEvent EVENT_IN_SAFARI_ZONE
  295. jr z, .notSafariZone
  296. callba SafariZoneCheckSteps
  297. ld a, [wSafariZoneGameOver]
  298. and a
  299. jp nz, WarpFound2
  300. .notSafariZone
  301. ld a, [wIsInBattle]
  302. and a
  303. jp nz, CheckWarpsNoCollision
  304. predef ApplyOutOfBattlePoisonDamage ; also increment daycare mon exp
  305. ld a, [wOutOfBattleBlackout]
  306. and a
  307. jp nz, HandleBlackOut ; if all pokemon fainted
  308. .newBattle
  309. call NewBattle
  310. ld hl, wd736
  311. res 2, [hl] ; standing on warp flag
  312. jp nc, CheckWarpsNoCollision ; check for warps if there was no battle
  313. .battleOccurred
  314. ld hl, wd72d
  315. res 6, [hl]
  316. ld hl, wFlags_D733
  317. res 3, [hl]
  318. ld hl, wCurrentMapScriptFlags
  319. set 5, [hl]
  320. set 6, [hl]
  321. xor a
  322. ld [hJoyHeld], a
  323. ld a, [wCurMap]
  324. cp CINNABAR_GYM
  325. jr nz, .notCinnabarGym
  326. SetEvent EVENT_2A7
  327. .notCinnabarGym
  328. ld hl, wd72e
  329. set 5, [hl]
  330. ld a, [wCurMap]
  331. cp OAKS_LAB
  332. jp z, .noFaintCheck ; no blacking out if the player lost to the rival in Oak's lab
  333. callab AnyPartyAlive
  334. ld a, d
  335. and a
  336. jr z, .allPokemonFainted
  337. .noFaintCheck
  338. ld c, 10
  339. call DelayFrames
  340. jp EnterMap
  341. .allPokemonFainted
  342. ld a, $ff
  343. ld [wIsInBattle], a
  344. call RunMapScript
  345. jp HandleBlackOut
  346. ; function to determine if there will be a battle and execute it (either a trainer battle or wild battle)
  347. ; sets carry if a battle occurred and unsets carry if not
  348. NewBattle::
  349. ld a, [wd72d]
  350. bit 4, a
  351. jr nz, .noBattle
  352. call IsPlayerCharacterBeingControlledByGame
  353. jr nz, .noBattle ; no battle if the player character is under the game's control
  354. ld a, [wd72e]
  355. bit 4, a
  356. jr nz, .noBattle
  357. jpba InitBattle
  358. .noBattle
  359. and a
  360. ret
  361. ; function to make bikes twice as fast as walking
  362. DoBikeSpeedup::
  363. ld a, [wNPCMovementScriptPointerTableNum]
  364. and a
  365. ret nz
  366. ld a, [wCurMap]
  367. cp ROUTE_17 ; Cycling Road
  368. jr nz, .goFaster
  369. ld a, [hJoyHeld]
  370. and D_UP | D_LEFT | D_RIGHT
  371. ret nz
  372. .goFaster
  373. jp AdvancePlayerSprite
  374. ; check if the player has stepped onto a warp after having not collided
  375. CheckWarpsNoCollision::
  376. ld a, [wNumberOfWarps]
  377. and a
  378. jp z, CheckMapConnections
  379. ld a, [wNumberOfWarps]
  380. ld b, 0
  381. ld c, a
  382. ld a, [wYCoord]
  383. ld d, a
  384. ld a, [wXCoord]
  385. ld e, a
  386. ld hl, wWarpEntries
  387. CheckWarpsNoCollisionLoop::
  388. ld a, [hli] ; check if the warp's Y position matches
  389. cp d
  390. jr nz, CheckWarpsNoCollisionRetry1
  391. ld a, [hli] ; check if the warp's X position matches
  392. cp e
  393. jr nz, CheckWarpsNoCollisionRetry2
  394. ; if a match was found
  395. push hl
  396. push bc
  397. ld hl, wd736
  398. set 2, [hl] ; standing on warp flag
  399. callba IsPlayerStandingOnDoorTileOrWarpTile
  400. pop bc
  401. pop hl
  402. jr c, WarpFound1 ; jump if standing on door or warp
  403. push hl
  404. push bc
  405. call ExtraWarpCheck
  406. pop bc
  407. pop hl
  408. jr nc, CheckWarpsNoCollisionRetry2
  409. ; if the extra check passed
  410. ld a, [wFlags_D733]
  411. bit 2, a
  412. jr nz, WarpFound1
  413. push de
  414. push bc
  415. call Joypad
  416. pop bc
  417. pop de
  418. ld a, [hJoyHeld]
  419. and D_DOWN | D_UP | D_LEFT | D_RIGHT
  420. jr z, CheckWarpsNoCollisionRetry2 ; if directional buttons aren't being pressed, do not pass through the warp
  421. jr WarpFound1
  422. ; check if the player has stepped onto a warp after having collided
  423. CheckWarpsCollision::
  424. ld a, [wNumberOfWarps]
  425. ld c, a
  426. ld hl, wWarpEntries
  427. .loop
  428. ld a, [hli] ; Y coordinate of warp
  429. ld b, a
  430. ld a, [wYCoord]
  431. cp b
  432. jr nz, .retry1
  433. ld a, [hli] ; X coordinate of warp
  434. ld b, a
  435. ld a, [wXCoord]
  436. cp b
  437. jr nz, .retry2
  438. ld a, [hli]
  439. ld [wDestinationWarpID], a
  440. ld a, [hl]
  441. ld [hWarpDestinationMap], a
  442. jr WarpFound2
  443. .retry1
  444. inc hl
  445. .retry2
  446. inc hl
  447. inc hl
  448. dec c
  449. jr nz, .loop
  450. jp OverworldLoop
  451. CheckWarpsNoCollisionRetry1::
  452. inc hl
  453. CheckWarpsNoCollisionRetry2::
  454. inc hl
  455. inc hl
  456. jp ContinueCheckWarpsNoCollisionLoop
  457. WarpFound1::
  458. ld a, [hli]
  459. ld [wDestinationWarpID], a
  460. ld a, [hli]
  461. ld [hWarpDestinationMap], a
  462. WarpFound2::
  463. ld a, [wNumberOfWarps]
  464. sub c
  465. ld [wWarpedFromWhichWarp], a ; save ID of used warp
  466. ld a, [wCurMap]
  467. ld [wWarpedFromWhichMap], a
  468. call CheckIfInOutsideMap
  469. jr nz, .indoorMaps
  470. ; this is for handling "outside" maps that can't have the 0xFF destination map
  471. ld a, [wCurMap]
  472. ld [wLastMap], a
  473. ld a, [wCurMapWidth]
  474. ld [wUnusedD366], a ; not read
  475. ld a, [hWarpDestinationMap]
  476. ld [wCurMap], a
  477. cp ROCK_TUNNEL_1
  478. jr nz, .notRockTunnel
  479. ld a, $06
  480. ld [wMapPalOffset], a
  481. call GBFadeOutToBlack
  482. .notRockTunnel
  483. call PlayMapChangeSound
  484. jr .done
  485. ; for maps that can have the 0xFF destination map, which means to return to the outside map
  486. ; not all these maps are necessarily indoors, though
  487. .indoorMaps
  488. ld a, [hWarpDestinationMap] ; destination map
  489. cp $ff
  490. jr z, .goBackOutside
  491. ; if not going back to the previous map
  492. ld [wCurMap], a
  493. callba IsPlayerStandingOnWarpPadOrHole
  494. ld a, [wStandingOnWarpPadOrHole]
  495. dec a ; is the player on a warp pad?
  496. jr nz, .notWarpPad
  497. ; if the player is on a warp pad
  498. ld hl, wd732
  499. set 3, [hl]
  500. call LeaveMapAnim
  501. jr .skipMapChangeSound
  502. .notWarpPad
  503. call PlayMapChangeSound
  504. .skipMapChangeSound
  505. ld hl, wd736
  506. res 0, [hl]
  507. res 1, [hl]
  508. jr .done
  509. .goBackOutside
  510. ld a, [wLastMap]
  511. ld [wCurMap], a
  512. call PlayMapChangeSound
  513. xor a
  514. ld [wMapPalOffset], a
  515. .done
  516. ld hl, wd736
  517. set 0, [hl] ; have the player's sprite step out from the door (if there is one)
  518. call IgnoreInputForHalfSecond
  519. jp EnterMap
  520. ContinueCheckWarpsNoCollisionLoop::
  521. inc b ; increment warp number
  522. dec c ; decrement number of warps
  523. jp nz, CheckWarpsNoCollisionLoop
  524. ; if no matching warp was found
  525. CheckMapConnections::
  526. .checkWestMap
  527. ld a, [wXCoord]
  528. cp $ff
  529. jr nz, .checkEastMap
  530. ld a, [wMapConn3Ptr]
  531. ld [wCurMap], a
  532. ld a, [wWestConnectedMapXAlignment] ; new X coordinate upon entering west map
  533. ld [wXCoord], a
  534. ld a, [wYCoord]
  535. ld c, a
  536. ld a, [wWestConnectedMapYAlignment] ; Y adjustment upon entering west map
  537. add c
  538. ld c, a
  539. ld [wYCoord], a
  540. ld a, [wWestConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for Y position
  541. ld l, a
  542. ld a, [wWestConnectedMapViewPointer + 1]
  543. ld h, a
  544. srl c
  545. jr z, .savePointer1
  546. .pointerAdjustmentLoop1
  547. ld a, [wWestConnectedMapWidth] ; width of connected map
  548. add MAP_BORDER * 2
  549. ld e, a
  550. ld d, 0
  551. ld b, 0
  552. add hl, de
  553. dec c
  554. jr nz, .pointerAdjustmentLoop1
  555. .savePointer1
  556. ld a, l
  557. ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
  558. ld a, h
  559. ld [wCurrentTileBlockMapViewPointer + 1], a
  560. jp .loadNewMap
  561. .checkEastMap
  562. ld b, a
  563. ld a, [wCurrentMapWidth2] ; map width
  564. cp b
  565. jr nz, .checkNorthMap
  566. ld a, [wMapConn4Ptr]
  567. ld [wCurMap], a
  568. ld a, [wEastConnectedMapXAlignment] ; new X coordinate upon entering east map
  569. ld [wXCoord], a
  570. ld a, [wYCoord]
  571. ld c, a
  572. ld a, [wEastConnectedMapYAlignment] ; Y adjustment upon entering east map
  573. add c
  574. ld c, a
  575. ld [wYCoord], a
  576. ld a, [wEastConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for Y position
  577. ld l, a
  578. ld a, [wEastConnectedMapViewPointer + 1]
  579. ld h, a
  580. srl c
  581. jr z, .savePointer2
  582. .pointerAdjustmentLoop2
  583. ld a, [wEastConnectedMapWidth]
  584. add MAP_BORDER * 2
  585. ld e, a
  586. ld d, 0
  587. ld b, 0
  588. add hl, de
  589. dec c
  590. jr nz, .pointerAdjustmentLoop2
  591. .savePointer2
  592. ld a, l
  593. ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
  594. ld a, h
  595. ld [wCurrentTileBlockMapViewPointer + 1], a
  596. jp .loadNewMap
  597. .checkNorthMap
  598. ld a, [wYCoord]
  599. cp $ff
  600. jr nz, .checkSouthMap
  601. ld a, [wMapConn1Ptr]
  602. ld [wCurMap], a
  603. ld a, [wNorthConnectedMapYAlignment] ; new Y coordinate upon entering north map
  604. ld [wYCoord], a
  605. ld a, [wXCoord]
  606. ld c, a
  607. ld a, [wNorthConnectedMapXAlignment] ; X adjustment upon entering north map
  608. add c
  609. ld c, a
  610. ld [wXCoord], a
  611. ld a, [wNorthConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for X position
  612. ld l, a
  613. ld a, [wNorthConnectedMapViewPointer + 1]
  614. ld h, a
  615. ld b, 0
  616. srl c
  617. add hl, bc
  618. ld a, l
  619. ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
  620. ld a, h
  621. ld [wCurrentTileBlockMapViewPointer + 1], a
  622. jp .loadNewMap
  623. .checkSouthMap
  624. ld b, a
  625. ld a, [wCurrentMapHeight2]
  626. cp b
  627. jr nz, .didNotEnterConnectedMap
  628. ld a, [wMapConn2Ptr]
  629. ld [wCurMap], a
  630. ld a, [wSouthConnectedMapYAlignment] ; new Y coordinate upon entering south map
  631. ld [wYCoord], a
  632. ld a, [wXCoord]
  633. ld c, a
  634. ld a, [wSouthConnectedMapXAlignment] ; X adjustment upon entering south map
  635. add c
  636. ld c, a
  637. ld [wXCoord], a
  638. ld a, [wSouthConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for X position
  639. ld l, a
  640. ld a, [wSouthConnectedMapViewPointer + 1]
  641. ld h, a
  642. ld b, 0
  643. srl c
  644. add hl, bc
  645. ld a, l
  646. ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
  647. ld a, h
  648. ld [wCurrentTileBlockMapViewPointer + 1], a
  649. .loadNewMap ; load the connected map that was entered
  650. call LoadMapHeader
  651. call PlayDefaultMusicFadeOutCurrent
  652. ld b, SET_PAL_OVERWORLD
  653. call RunPaletteCommand
  654. ; Since the sprite set shouldn't change, this will just update VRAM slots at
  655. ; $C2XE without loading any tile patterns.
  656. callba InitMapSprites
  657. call LoadTileBlockMap
  658. jp OverworldLoopLessDelay
  659. .didNotEnterConnectedMap
  660. jp OverworldLoop
  661. ; function to play a sound when changing maps
  662. PlayMapChangeSound::
  663. aCoord 8, 8 ; upper left tile of the 4x4 square the player's sprite is standing on
  664. cp $0b ; door tile in tileset 0
  665. jr nz, .didNotGoThroughDoor
  666. ld a, SFX_GO_INSIDE
  667. jr .playSound
  668. .didNotGoThroughDoor
  669. ld a, SFX_GO_OUTSIDE
  670. .playSound
  671. call PlaySound
  672. ld a, [wMapPalOffset]
  673. and a
  674. ret nz
  675. jp GBFadeOutToBlack
  676. CheckIfInOutsideMap::
  677. ; If the player is in an outside map (a town or route), set the z flag
  678. ld a, [wCurMapTileset]
  679. and a ; most towns/routes have tileset 0 (OVERWORLD)
  680. ret z
  681. cp PLATEAU ; Route 23 / Indigo Plateau
  682. ret
  683. ; this function is an extra check that sometimes has to pass in order to warp, beyond just standing on a warp
  684. ; the "sometimes" qualification is necessary because of CheckWarpsNoCollision's behavior
  685. ; depending on the map, either "function 1" or "function 2" is used for the check
  686. ; "function 1" passes when the player is at the edge of the map and is facing towards the outside of the map
  687. ; "function 2" passes when the the tile in front of the player is among a certain set
  688. ; sets carry if the check passes, otherwise clears carry
  689. ExtraWarpCheck::
  690. ld a, [wCurMap]
  691. cp SS_ANNE_3
  692. jr z, .useFunction1
  693. cp ROCKET_HIDEOUT_1
  694. jr z, .useFunction2
  695. cp ROCKET_HIDEOUT_2
  696. jr z, .useFunction2
  697. cp ROCKET_HIDEOUT_4
  698. jr z, .useFunction2
  699. cp ROCK_TUNNEL_1
  700. jr z, .useFunction2
  701. ld a, [wCurMapTileset]
  702. and a ; outside tileset (OVERWORLD)
  703. jr z, .useFunction2
  704. cp SHIP ; S.S. Anne tileset
  705. jr z, .useFunction2
  706. cp SHIP_PORT ; Vermilion Port tileset
  707. jr z, .useFunction2
  708. cp PLATEAU ; Indigo Plateau tileset
  709. jr z, .useFunction2
  710. .useFunction1
  711. ld hl, IsPlayerFacingEdgeOfMap
  712. jr .doBankswitch
  713. .useFunction2
  714. ld hl, IsWarpTileInFrontOfPlayer
  715. .doBankswitch
  716. ld b, BANK(IsWarpTileInFrontOfPlayer)
  717. jp Bankswitch
  718. MapEntryAfterBattle::
  719. callba IsPlayerStandingOnWarp ; for enabling warp testing after collisions
  720. ld a, [wMapPalOffset]
  721. and a
  722. jp z, GBFadeInFromWhite
  723. jp LoadGBPal
  724. HandleBlackOut::
  725. ; For when all the player's pokemon faint.
  726. ; Does not print the "blacked out" message.
  727. call GBFadeOutToBlack
  728. ld a, $08
  729. call StopMusic
  730. ld hl, wd72e
  731. res 5, [hl]
  732. ld a, Bank(ResetStatusAndHalveMoneyOnBlackout) ; also Bank(SpecialWarpIn) and Bank(SpecialEnterMap)
  733. ld [H_LOADEDROMBANK], a
  734. ld [MBC1RomBank], a
  735. call ResetStatusAndHalveMoneyOnBlackout
  736. call SpecialWarpIn
  737. call PlayDefaultMusicFadeOutCurrent
  738. jp SpecialEnterMap
  739. StopMusic::
  740. ld [wAudioFadeOutControl], a
  741. ld a, $ff
  742. ld [wNewSoundID], a
  743. call PlaySound
  744. .wait
  745. ld a, [wAudioFadeOutControl]
  746. and a
  747. jr nz, .wait
  748. jp StopAllSounds
  749. HandleFlyWarpOrDungeonWarp::
  750. call UpdateSprites
  751. call Delay3
  752. xor a
  753. ld [wBattleResult], a
  754. ld [wWalkBikeSurfState], a
  755. ld [wIsInBattle], a
  756. ld [wMapPalOffset], a
  757. ld hl, wd732
  758. set 2, [hl] ; fly warp or dungeon warp
  759. res 5, [hl] ; forced to ride bike
  760. call LeaveMapAnim
  761. ld a, Bank(SpecialWarpIn)
  762. ld [H_LOADEDROMBANK], a
  763. ld [MBC1RomBank], a
  764. call SpecialWarpIn
  765. jp SpecialEnterMap
  766. LeaveMapAnim::
  767. jpba _LeaveMapAnim
  768. LoadPlayerSpriteGraphics::
  769. ; Load sprite graphics based on whether the player is standing, biking, or surfing.
  770. ; 0: standing
  771. ; 1: biking
  772. ; 2: surfing
  773. ld a, [wWalkBikeSurfState]
  774. dec a
  775. jr z, .ridingBike
  776. ld a, [hTilesetType]
  777. and a
  778. jr nz, .determineGraphics
  779. jr .startWalking
  780. .ridingBike
  781. ; If the bike can't be used,
  782. ; start walking instead.
  783. call IsBikeRidingAllowed
  784. jr c, .determineGraphics
  785. .startWalking
  786. xor a
  787. ld [wWalkBikeSurfState], a
  788. ld [wWalkBikeSurfStateCopy], a
  789. jp LoadWalkingPlayerSpriteGraphics
  790. .determineGraphics
  791. ld a, [wWalkBikeSurfState]
  792. and a
  793. jp z, LoadWalkingPlayerSpriteGraphics
  794. dec a
  795. jp z, LoadBikePlayerSpriteGraphics
  796. dec a
  797. jp z, LoadSurfingPlayerSpriteGraphics
  798. jp LoadWalkingPlayerSpriteGraphics
  799. IsBikeRidingAllowed::
  800. ; The bike can be used on Route 23 and Indigo Plateau,
  801. ; or maps with tilesets in BikeRidingTilesets.
  802. ; Return carry if biking is allowed.
  803. ld a, [wCurMap]
  804. cp ROUTE_23
  805. jr z, .allowed
  806. cp INDIGO_PLATEAU
  807. jr z, .allowed
  808. ld a, [wCurMapTileset]
  809. ld b, a
  810. ld hl, BikeRidingTilesets
  811. .loop
  812. ld a, [hli]
  813. cp b
  814. jr z, .allowed
  815. inc a
  816. jr nz, .loop
  817. and a
  818. ret
  819. .allowed
  820. scf
  821. ret
  822. INCLUDE "data/bike_riding_tilesets.asm"
  823. ; load the tile pattern data of the current tileset into VRAM
  824. LoadTilesetTilePatternData::
  825. ld a, [wTilesetGfxPtr]
  826. ld l, a
  827. ld a, [wTilesetGfxPtr + 1]
  828. ld h, a
  829. ld de, vTileset
  830. ld bc, $600
  831. ld a, [wTilesetBank]
  832. jp FarCopyData2
  833. ; this loads the current maps complete tile map (which references blocks, not individual tiles) to C6E8
  834. ; it can also load partial tile maps of connected maps into a border of length 3 around the current map
  835. LoadTileBlockMap::
  836. ; fill C6E8-CBFB with the background tile
  837. ld hl, wOverworldMap
  838. ld a, [wMapBackgroundTile]
  839. ld d, a
  840. ld bc, $0514
  841. .backgroundTileLoop
  842. ld a, d
  843. ld [hli], a
  844. dec bc
  845. ld a, c
  846. or b
  847. jr nz, .backgroundTileLoop
  848. ; load tile map of current map (made of tile block IDs)
  849. ; a 3-byte border at the edges of the map is kept so that there is space for map connections
  850. ld hl, wOverworldMap
  851. ld a, [wCurMapWidth]
  852. ld [hMapWidth], a
  853. add MAP_BORDER * 2 ; east and west
  854. ld [hMapStride], a ; map width + border
  855. ld b, 0
  856. ld c, a
  857. ; make space for north border (next 3 lines)
  858. add hl, bc
  859. add hl, bc
  860. add hl, bc
  861. ld c, MAP_BORDER
  862. add hl, bc ; this puts us past the (west) border
  863. ld a, [wMapDataPtr] ; tile map pointer
  864. ld e, a
  865. ld a, [wMapDataPtr + 1]
  866. ld d, a ; de = tile map pointer
  867. ld a, [wCurMapHeight]
  868. ld b, a
  869. .rowLoop ; copy one row each iteration
  870. push hl
  871. ld a, [hMapWidth] ; map width (without border)
  872. ld c, a
  873. .rowInnerLoop
  874. ld a, [de]
  875. inc de
  876. ld [hli], a
  877. dec c
  878. jr nz, .rowInnerLoop
  879. ; add the map width plus the border to the base address of the current row to get the next row's address
  880. pop hl
  881. ld a, [hMapStride] ; map width + border
  882. add l
  883. ld l, a
  884. jr nc, .noCarry
  885. inc h
  886. .noCarry
  887. dec b
  888. jr nz, .rowLoop
  889. .northConnection
  890. ld a, [wMapConn1Ptr]
  891. cp $ff
  892. jr z, .southConnection
  893. call SwitchToMapRomBank
  894. ld a, [wNorthConnectionStripSrc]
  895. ld l, a
  896. ld a, [wNorthConnectionStripSrc + 1]
  897. ld h, a
  898. ld a, [wNorthConnectionStripDest]
  899. ld e, a
  900. ld a, [wNorthConnectionStripDest + 1]
  901. ld d, a
  902. ld a, [wNorthConnectionStripWidth]
  903. ld [hNorthSouthConnectionStripWidth], a
  904. ld a, [wNorthConnectedMapWidth]
  905. ld [hNorthSouthConnectedMapWidth], a
  906. call LoadNorthSouthConnectionsTileMap
  907. .southConnection
  908. ld a, [wMapConn2Ptr]
  909. cp $ff
  910. jr z, .westConnection
  911. call SwitchToMapRomBank
  912. ld a, [wSouthConnectionStripSrc]
  913. ld l, a
  914. ld a, [wSouthConnectionStripSrc + 1]
  915. ld h, a
  916. ld a, [wSouthConnectionStripDest]
  917. ld e, a
  918. ld a, [wSouthConnectionStripDest + 1]
  919. ld d, a
  920. ld a, [wSouthConnectionStripWidth]
  921. ld [hNorthSouthConnectionStripWidth], a
  922. ld a, [wSouthConnectedMapWidth]
  923. ld [hNorthSouthConnectedMapWidth], a
  924. call LoadNorthSouthConnectionsTileMap
  925. .westConnection
  926. ld a, [wMapConn3Ptr]
  927. cp $ff
  928. jr z, .eastConnection
  929. call SwitchToMapRomBank
  930. ld a, [wWestConnectionStripSrc]
  931. ld l, a
  932. ld a, [wWestConnectionStripSrc + 1]
  933. ld h, a
  934. ld a, [wWestConnectionStripDest]
  935. ld e, a
  936. ld a, [wWestConnectionStripDest + 1]
  937. ld d, a
  938. ld a, [wWestConnectionStripHeight]
  939. ld b, a
  940. ld a, [wWestConnectedMapWidth]
  941. ld [hEastWestConnectedMapWidth], a
  942. call LoadEastWestConnectionsTileMap
  943. .eastConnection
  944. ld a, [wMapConn4Ptr]
  945. cp $ff
  946. jr z, .done
  947. call SwitchToMapRomBank
  948. ld a, [wEastConnectionStripSrc]
  949. ld l, a
  950. ld a, [wEastConnectionStripSrc + 1]
  951. ld h, a
  952. ld a, [wEastConnectionStripDest]
  953. ld e, a
  954. ld a, [wEastConnectionStripDest + 1]
  955. ld d, a
  956. ld a, [wEastConnectionStripHeight]
  957. ld b, a
  958. ld a, [wEastConnectedMapWidth]
  959. ld [hEastWestConnectedMapWidth], a
  960. call LoadEastWestConnectionsTileMap
  961. .done
  962. ret
  963. LoadNorthSouthConnectionsTileMap::
  964. ld c, MAP_BORDER
  965. .loop
  966. push de
  967. push hl
  968. ld a, [hNorthSouthConnectionStripWidth]
  969. ld b, a
  970. .innerLoop
  971. ld a, [hli]
  972. ld [de], a
  973. inc de
  974. dec b
  975. jr nz, .innerLoop
  976. pop hl
  977. pop de
  978. ld a, [hNorthSouthConnectedMapWidth]
  979. add l
  980. ld l, a
  981. jr nc, .noCarry1
  982. inc h
  983. .noCarry1
  984. ld a, [wCurMapWidth]
  985. add MAP_BORDER * 2
  986. add e
  987. ld e, a
  988. jr nc, .noCarry2
  989. inc d
  990. .noCarry2
  991. dec c
  992. jr nz, .loop
  993. ret
  994. LoadEastWestConnectionsTileMap::
  995. push hl
  996. push de
  997. ld c, MAP_BORDER
  998. .innerLoop
  999. ld a, [hli]
  1000. ld [de], a
  1001. inc de
  1002. dec c
  1003. jr nz, .innerLoop
  1004. pop de
  1005. pop hl
  1006. ld a, [hEastWestConnectedMapWidth]
  1007. add l
  1008. ld l, a
  1009. jr nc, .noCarry1
  1010. inc h
  1011. .noCarry1
  1012. ld a, [wCurMapWidth]
  1013. add MAP_BORDER * 2
  1014. add e
  1015. ld e, a
  1016. jr nc, .noCarry2
  1017. inc d
  1018. .noCarry2
  1019. dec b
  1020. jr nz, LoadEastWestConnectionsTileMap
  1021. ret
  1022. ; function to check if there is a sign or sprite in front of the player
  1023. ; if so, it is stored in [hSpriteIndexOrTextID]
  1024. ; if not, [hSpriteIndexOrTextID] is set to 0
  1025. IsSpriteOrSignInFrontOfPlayer::
  1026. xor a
  1027. ld [hSpriteIndexOrTextID], a
  1028. ld a, [wNumSigns]
  1029. and a
  1030. jr z, .extendRangeOverCounter
  1031. ; if there are signs
  1032. predef GetTileAndCoordsInFrontOfPlayer ; get the coordinates in front of the player in de
  1033. ld hl, wSignCoords
  1034. ld a, [wNumSigns]
  1035. ld b, a
  1036. ld c, 0
  1037. .signLoop
  1038. inc c
  1039. ld a, [hli] ; sign Y
  1040. cp d
  1041. jr z, .yCoordMatched
  1042. inc hl
  1043. jr .retry
  1044. .yCoordMatched
  1045. ld a, [hli] ; sign X
  1046. cp e
  1047. jr nz, .retry
  1048. .xCoordMatched
  1049. ; found sign
  1050. push hl
  1051. push bc
  1052. ld hl, wSignTextIDs
  1053. ld b, 0
  1054. dec c
  1055. add hl, bc
  1056. ld a, [hl]
  1057. ld [hSpriteIndexOrTextID], a ; store sign text ID
  1058. pop bc
  1059. pop hl
  1060. ret
  1061. .retry
  1062. dec b
  1063. jr nz, .signLoop
  1064. ; check if the player is front of a counter in a pokemon center, pokemart, etc. and if so, extend the range at which he can talk to the NPC
  1065. .extendRangeOverCounter
  1066. predef GetTileAndCoordsInFrontOfPlayer ; get the tile in front of the player in c
  1067. ld hl, wTilesetTalkingOverTiles ; list of tiles that extend talking range (counter tiles)
  1068. ld b, 3
  1069. ld d, $20 ; talking range in pixels (long range)
  1070. .counterTilesLoop
  1071. ld a, [hli]
  1072. cp c
  1073. jr z, IsSpriteInFrontOfPlayer2 ; jumps if the tile in front of the player is a counter tile
  1074. dec b
  1075. jr nz, .counterTilesLoop
  1076. ; part of the above function, but sometimes its called on its own, when signs are irrelevant
  1077. ; the caller must zero [hSpriteIndexOrTextID]
  1078. IsSpriteInFrontOfPlayer::
  1079. ld d, $10 ; talking range in pixels (normal range)
  1080. IsSpriteInFrontOfPlayer2::
  1081. lb bc, $3c, $40 ; Y and X position of player sprite
  1082. ld a, [wSpriteStateData1 + 9] ; direction the player is facing
  1083. .checkIfPlayerFacingUp
  1084. cp SPRITE_FACING_UP
  1085. jr nz, .checkIfPlayerFacingDown
  1086. ; facing up
  1087. ld a, b
  1088. sub d
  1089. ld b, a
  1090. ld a, PLAYER_DIR_UP
  1091. jr .doneCheckingDirection
  1092. .checkIfPlayerFacingDown
  1093. cp SPRITE_FACING_DOWN
  1094. jr nz, .checkIfPlayerFacingRight
  1095. ; facing down
  1096. ld a, b
  1097. add d
  1098. ld b, a
  1099. ld a, PLAYER_DIR_DOWN
  1100. jr .doneCheckingDirection
  1101. .checkIfPlayerFacingRight
  1102. cp SPRITE_FACING_RIGHT
  1103. jr nz, .playerFacingLeft
  1104. ; facing right
  1105. ld a, c
  1106. add d
  1107. ld c, a
  1108. ld a, PLAYER_DIR_RIGHT
  1109. jr .doneCheckingDirection
  1110. .playerFacingLeft
  1111. ; facing left
  1112. ld a, c
  1113. sub d
  1114. ld c, a
  1115. ld a, PLAYER_DIR_LEFT
  1116. .doneCheckingDirection
  1117. ld [wPlayerDirection], a
  1118. ld a, [wNumSprites] ; number of sprites
  1119. and a
  1120. ret z
  1121. ; if there are sprites
  1122. ld hl, wSpriteStateData1 + $10
  1123. ld d, a
  1124. ld e, $01
  1125. .spriteLoop
  1126. push hl
  1127. ld a, [hli] ; image (0 if no sprite)
  1128. and a
  1129. jr z, .nextSprite
  1130. inc l
  1131. ld a, [hli] ; sprite visibility
  1132. inc a
  1133. jr z, .nextSprite
  1134. inc l
  1135. ld a, [hli] ; Y location
  1136. cp b
  1137. jr nz, .nextSprite
  1138. inc l
  1139. ld a, [hl] ; X location
  1140. cp c
  1141. jr z, .foundSpriteInFrontOfPlayer
  1142. .nextSprite
  1143. pop hl
  1144. ld a, l
  1145. add $10
  1146. ld l, a
  1147. inc e
  1148. dec d
  1149. jr nz, .spriteLoop
  1150. ret
  1151. .foundSpriteInFrontOfPlayer
  1152. pop hl
  1153. ld a, l
  1154. and $f0
  1155. inc a
  1156. ld l, a ; hl = $c1x1
  1157. set 7, [hl] ; set flag to make the sprite face the player
  1158. ld a, e
  1159. ld [hSpriteIndexOrTextID], a
  1160. ret
  1161. ; function to check if the player will jump down a ledge and check if the tile ahead is passable (when not surfing)
  1162. ; sets the carry flag if there is a collision, and unsets it if there isn't a collision
  1163. CollisionCheckOnLand::
  1164. ld a, [wd736]
  1165. bit 6, a ; is the player jumping?
  1166. jr nz, .noCollision
  1167. ; if not jumping a ledge
  1168. ld a, [wSimulatedJoypadStatesIndex]
  1169. and a
  1170. jr nz, .noCollision ; no collisions when the player's movements are being controlled by the game
  1171. ld a, [wPlayerDirection] ; the direction that the player is trying to go in
  1172. ld d, a
  1173. ld a, [wSpriteStateData1 + 12] ; the player sprite's collision data (bit field) (set in the sprite movement code)
  1174. and d ; check if a sprite is in the direction the player is trying to go
  1175. jr nz, .collision
  1176. xor a
  1177. ld [hSpriteIndexOrTextID], a
  1178. call IsSpriteInFrontOfPlayer ; check for sprite collisions again? when does the above check fail to detect a sprite collision?
  1179. ld a, [hSpriteIndexOrTextID]
  1180. and a ; was there a sprite collision?
  1181. jr nz, .collision
  1182. ; if no sprite collision
  1183. ld hl, TilePairCollisionsLand
  1184. call CheckForJumpingAndTilePairCollisions
  1185. jr c, .collision
  1186. call CheckTilePassable
  1187. jr nc, .noCollision
  1188. .collision
  1189. ld a, [wChannelSoundIDs + Ch4]
  1190. cp SFX_COLLISION ; check if collision sound is already playing
  1191. jr z, .setCarry
  1192. ld a, SFX_COLLISION
  1193. call PlaySound ; play collision sound (if it's not already playing)
  1194. .setCarry
  1195. scf
  1196. ret
  1197. .noCollision
  1198. and a
  1199. ret
  1200. ; function that checks if the tile in front of the player is passable
  1201. ; clears carry if it is, sets carry if not
  1202. CheckTilePassable::
  1203. predef GetTileAndCoordsInFrontOfPlayer ; get tile in front of player
  1204. ld a, [wTileInFrontOfPlayer] ; tile in front of player
  1205. ld c, a
  1206. ld hl, wTilesetCollisionPtr ; pointer to list of passable tiles
  1207. ld a, [hli]
  1208. ld h, [hl]
  1209. ld l, a ; hl now points to passable tiles
  1210. .loop
  1211. ld a, [hli]
  1212. cp $ff
  1213. jr z, .tileNotPassable
  1214. cp c
  1215. ret z
  1216. jr .loop
  1217. .tileNotPassable
  1218. scf
  1219. ret
  1220. ; check if the player is going to jump down a small ledge
  1221. ; and check for collisions that only occur between certain pairs of tiles
  1222. ; Input: hl - address of directional collision data
  1223. ; sets carry if there is a collision and unsets carry if not
  1224. CheckForJumpingAndTilePairCollisions::
  1225. push hl
  1226. predef GetTileAndCoordsInFrontOfPlayer ; get the tile in front of the player
  1227. push de
  1228. push bc
  1229. callba HandleLedges ; check if the player is trying to jump a ledge
  1230. pop bc
  1231. pop de
  1232. pop hl
  1233. and a
  1234. ld a, [wd736]
  1235. bit 6, a ; is the player jumping?
  1236. ret nz
  1237. ; if not jumping
  1238. CheckForTilePairCollisions2::
  1239. aCoord 8, 9 ; tile the player is on
  1240. ld [wTilePlayerStandingOn], a
  1241. CheckForTilePairCollisions::
  1242. ld a, [wTileInFrontOfPlayer]
  1243. ld c, a
  1244. .tilePairCollisionLoop
  1245. ld a, [wCurMapTileset] ; tileset number
  1246. ld b, a
  1247. ld a, [hli]
  1248. cp $ff
  1249. jr z, .noMatch
  1250. cp b
  1251. jr z, .tilesetMatches
  1252. inc hl
  1253. .retry
  1254. inc hl
  1255. jr .tilePairCollisionLoop
  1256. .tilesetMatches
  1257. ld a, [wTilePlayerStandingOn] ; tile the player is on
  1258. ld b, a
  1259. ld a, [hl]
  1260. cp b
  1261. jr z, .currentTileMatchesFirstInPair
  1262. inc hl
  1263. ld a, [hl]
  1264. cp b
  1265. jr z, .currentTileMatchesSecondInPair
  1266. jr .retry
  1267. .currentTileMatchesFirstInPair
  1268. inc hl
  1269. ld a, [hl]
  1270. cp c
  1271. jr z, .foundMatch
  1272. jr .tilePairCollisionLoop
  1273. .currentTileMatchesSecondInPair
  1274. dec hl
  1275. ld a, [hli]
  1276. cp c
  1277. inc hl
  1278. jr nz, .tilePairCollisionLoop
  1279. .foundMatch
  1280. scf
  1281. ret
  1282. .noMatch
  1283. and a
  1284. ret
  1285. ; FORMAT: tileset number, tile 1, tile 2
  1286. ; terminated by 0xFF
  1287. ; these entries indicate that the player may not cross between tile 1 and tile 2
  1288. ; it's mainly used to simulate differences in elevation
  1289. TilePairCollisionsLand::
  1290. db CAVERN, $20, $05
  1291. db CAVERN, $41, $05
  1292. db FOREST, $30, $2E
  1293. db CAVERN, $2A, $05
  1294. db CAVERN, $05, $21
  1295. db FOREST, $52, $2E
  1296. db FOREST, $55, $2E
  1297. db FOREST, $56, $2E
  1298. db FOREST, $20, $2E
  1299. db FOREST, $5E, $2E
  1300. db FOREST, $5F, $2E
  1301. db $FF
  1302. TilePairCollisionsWater::
  1303. db FOREST, $14, $2E
  1304. db FOREST, $48, $2E
  1305. db CAVERN, $14, $05
  1306. db $FF
  1307. ; this builds a tile map from the tile block map based on the current X/Y coordinates of the player's character
  1308. LoadCurrentMapView::
  1309. ld a, [H_LOADEDROMBANK]
  1310. push af
  1311. ld a, [wTilesetBank] ; tile data ROM bank
  1312. ld [H_LOADEDROMBANK], a
  1313. ld [MBC1RomBank], a ; switch to ROM bank that contains tile data
  1314. ld a, [wCurrentTileBlockMapViewPointer] ; address of upper left corner of current map view
  1315. ld e, a
  1316. ld a, [wCurrentTileBlockMapViewPointer + 1]
  1317. ld d, a
  1318. ld hl, wTileMapBackup
  1319. ld b, $05
  1320. .rowLoop ; each loop iteration fills in one row of tile blocks
  1321. push hl
  1322. push de
  1323. ld c, $06
  1324. .rowInnerLoop ; loop to draw each tile block of the current row
  1325. push bc
  1326. push de
  1327. push hl
  1328. ld a, [de]
  1329. ld c, a ; tile block number
  1330. call DrawTileBlock
  1331. pop hl
  1332. pop de
  1333. pop bc
  1334. inc hl
  1335. inc hl
  1336. inc hl
  1337. inc hl
  1338. inc de
  1339. dec c
  1340. jr nz, .rowInnerLoop
  1341. ; update tile block map pointer to next row's address
  1342. pop de
  1343. ld a, [wCurMapWidth]
  1344. add MAP_BORDER * 2
  1345. add e
  1346. ld e, a
  1347. jr nc, .noCarry
  1348. inc d
  1349. .noCarry
  1350. ; update tile map pointer to next row's address
  1351. pop hl
  1352. ld a, $60
  1353. add l
  1354. ld l, a
  1355. jr nc, .noCarry2
  1356. inc h
  1357. .noCarry2
  1358. dec b
  1359. jr nz, .rowLoop
  1360. ld hl, wTileMapBackup
  1361. ld bc, $0000
  1362. .adjustForYCoordWithinTileBlock
  1363. ld a, [wYBlockCoord]
  1364. and a
  1365. jr z, .adjustForXCoordWithinTileBlock
  1366. ld bc, $0030
  1367. add hl, bc
  1368. .adjustForXCoordWithinTileBlock
  1369. ld a, [wXBlockCoord]
  1370. and a
  1371. jr z, .copyToVisibleAreaBuffer
  1372. ld bc, $0002
  1373. add hl, bc
  1374. .copyToVisibleAreaBuffer
  1375. coord de, 0, 0 ; base address for the tiles that are directly transferred to VRAM during V-blank
  1376. ld b, SCREEN_HEIGHT
  1377. .rowLoop2
  1378. ld c, SCREEN_WIDTH
  1379. .rowInnerLoop2
  1380. ld a, [hli]
  1381. ld [de], a
  1382. inc de
  1383. dec c
  1384. jr nz, .rowInnerLoop2
  1385. ld a, $04
  1386. add l
  1387. ld l, a
  1388. jr nc, .noCarry3
  1389. inc h
  1390. .noCarry3
  1391. dec b
  1392. jr nz, .rowLoop2
  1393. pop af
  1394. ld [H_LOADEDROMBANK], a
  1395. ld [MBC1RomBank], a ; restore previous ROM bank
  1396. ret
  1397. AdvancePlayerSprite::
  1398. ld a, [wSpriteStateData1 + 3] ; delta Y
  1399. ld b, a
  1400. ld a, [wSpriteStateData1 + 5] ; delta X
  1401. ld c, a
  1402. ld hl, wWalkCounter ; walking animation counter
  1403. dec [hl]
  1404. jr nz, .afterUpdateMapCoords
  1405. ; if it's the end of the animation, update the player's map coordinates
  1406. ld a, [wYCoord]
  1407. add b
  1408. ld [wYCoord], a
  1409. ld a, [wXCoord]
  1410. add c
  1411. ld [wXCoord], a
  1412. .afterUpdateMapCoords
  1413. ld a, [wWalkCounter] ; walking animation counter
  1414. cp $07
  1415. jp nz, .scrollBackgroundAndSprites
  1416. ; if this is the first iteration of the animation
  1417. ld a, c
  1418. cp $01
  1419. jr nz, .checkIfMovingWest
  1420. ; moving east
  1421. ld a, [wMapViewVRAMPointer]
  1422. ld e, a
  1423. and $e0
  1424. ld d, a
  1425. ld a, e
  1426. add $02
  1427. and $1f
  1428. or d
  1429. ld [wMapViewVRAMPointer], a
  1430. jr .adjustXCoordWithinBlock
  1431. .checkIfMovingWest
  1432. cp $ff
  1433. jr nz, .checkIfMovingSouth
  1434. ; moving west
  1435. ld a, [wMapViewVRAMPointer]
  1436. ld e, a
  1437. and $e0
  1438. ld d, a
  1439. ld a, e
  1440. sub $02
  1441. and $1f
  1442. or d
  1443. ld [wMapViewVRAMPointer], a
  1444. jr .adjustXCoordWithinBlock
  1445. .checkIfMovingSouth
  1446. ld a, b
  1447. cp $01
  1448. jr nz, .checkIfMovingNorth
  1449. ; moving south
  1450. ld a, [wMapViewVRAMPointer]
  1451. add $40
  1452. ld [wMapViewVRAMPointer], a
  1453. jr nc, .adjustXCoordWithinBlock
  1454. ld a, [wMapViewVRAMPointer + 1]
  1455. inc a
  1456. and $03
  1457. or $98
  1458. ld [wMapViewVRAMPointer + 1], a
  1459. jr .adjustXCoordWithinBlock
  1460. .checkIfMovingNorth
  1461. cp $ff
  1462. jr nz, .adjustXCoordWithinBlock
  1463. ; moving north
  1464. ld a, [wMapViewVRAMPointer]
  1465. sub $40
  1466. ld [wMapViewVRAMPointer], a
  1467. jr nc, .adjustXCoordWithinBlock
  1468. ld a, [wMapViewVRAMPointer + 1]
  1469. dec a
  1470. and $03
  1471. or $98
  1472. ld [wMapViewVRAMPointer + 1], a
  1473. .adjustXCoordWithinBlock
  1474. ld a, c
  1475. and a
  1476. jr z, .pointlessJump ; mistake?
  1477. .pointlessJump
  1478. ld hl, wXBlockCoord
  1479. ld a, [hl]
  1480. add c
  1481. ld [hl], a
  1482. cp $02
  1483. jr nz, .checkForMoveToWestBlock
  1484. ; moved into the tile block to the east
  1485. xor a
  1486. ld [hl], a
  1487. ld hl, wXOffsetSinceLastSpecialWarp
  1488. inc [hl]
  1489. ld de, wCurrentTileBlockMapViewPointer
  1490. call MoveTileBlockMapPointerEast
  1491. jr .updateMapView
  1492. .checkForMoveToWestBlock
  1493. cp $ff
  1494. jr nz, .adjustYCoordWithinBlock
  1495. ; moved into the tile block to the west
  1496. ld a, $01
  1497. ld [hl], a
  1498. ld hl, wXOffsetSinceLastSpecialWarp
  1499. dec [hl]
  1500. ld de, wCurrentTileBlockMapViewPointer
  1501. call MoveTileBlockMapPointerWest
  1502. jr .updateMapView
  1503. .adjustYCoordWithinBlock
  1504. ld hl, wYBlockCoord
  1505. ld a, [hl]
  1506. add b
  1507. ld [hl], a
  1508. cp $02
  1509. jr nz, .checkForMoveToNorthBlock
  1510. ; moved into the tile block to the south
  1511. xor a
  1512. ld [hl], a
  1513. ld hl, wYOffsetSinceLastSpecialWarp
  1514. inc [hl]
  1515. ld de, wCurrentTileBlockMapViewPointer
  1516. ld a, [wCurMapWidth]
  1517. call MoveTileBlockMapPointerSouth
  1518. jr .updateMapView
  1519. .checkForMoveToNorthBlock
  1520. cp $ff
  1521. jr nz, .updateMapView
  1522. ; moved into the tile block to the north
  1523. ld a, $01
  1524. ld [hl], a
  1525. ld hl, wYOffsetSinceLastSpecialWarp
  1526. dec [hl]
  1527. ld de, wCurrentTileBlockMapViewPointer
  1528. ld a, [wCurMapWidth]
  1529. call MoveTileBlockMapPointerNorth
  1530. .updateMapView
  1531. call LoadCurrentMapView
  1532. ld a, [wSpriteStateData1 + 3] ; delta Y
  1533. cp $01
  1534. jr nz, .checkIfMovingNorth2
  1535. ; if moving south
  1536. call ScheduleSouthRowRedraw
  1537. jr .scrollBackgroundAndSprites
  1538. .checkIfMovingNorth2
  1539. cp $ff
  1540. jr nz, .checkIfMovingEast2
  1541. ; if moving north
  1542. call ScheduleNorthRowRedraw
  1543. jr .scrollBackgroundAndSprites
  1544. .checkIfMovingEast2
  1545. ld a, [wSpriteStateData1 + 5] ; delta X
  1546. cp $01
  1547. jr nz, .checkIfMovingWest2
  1548. ; if moving east
  1549. call ScheduleEastColumnRedraw
  1550. jr .scrollBackgroundAndSprites
  1551. .checkIfMovingWest2
  1552. cp $ff
  1553. jr nz, .scrollBackgroundAndSprites
  1554. ; if moving west
  1555. call ScheduleWestColumnRedraw
  1556. .scrollBackgroundAndSprites
  1557. ld a, [wSpriteStateData1 + 3] ; delta Y
  1558. ld b, a
  1559. ld a, [wSpriteStateData1 + 5] ; delta X
  1560. ld c, a
  1561. sla b
  1562. sla c
  1563. ld a, [hSCY]
  1564. add b
  1565. ld [hSCY], a ; update background scroll Y
  1566. ld a, [hSCX]
  1567. add c
  1568. ld [hSCX], a ; update background scroll X
  1569. ; shift all the sprites in the direction opposite of the player's motion
  1570. ; so that the player appears to move relative to them
  1571. ld hl, wSpriteStateData1 + $14
  1572. ld a, [wNumSprites] ; number of sprites
  1573. and a ; are there any sprites?
  1574. jr z, .done
  1575. ld e, a
  1576. .spriteShiftLoop
  1577. ld a, [hl]
  1578. sub b
  1579. ld [hli], a
  1580. inc l
  1581. ld a, [hl]
  1582. sub c
  1583. ld [hl], a
  1584. ld a, $0e
  1585. add l
  1586. ld l, a
  1587. dec e
  1588. jr nz, .spriteShiftLoop
  1589. .done
  1590. ret
  1591. ; the following four functions are used to move the pointer to the upper left
  1592. ; corner of the tile block map in the direction of motion
  1593. MoveTileBlockMapPointerEast::
  1594. ld a, [de]
  1595. add $01
  1596. ld [de], a
  1597. ret nc
  1598. inc de
  1599. ld a, [de]
  1600. inc a
  1601. ld [de], a
  1602. ret
  1603. MoveTileBlockMapPointerWest::
  1604. ld a, [de]
  1605. sub $01
  1606. ld [de], a
  1607. ret nc
  1608. inc de
  1609. ld a, [de]
  1610. dec a
  1611. ld [de], a
  1612. ret
  1613. MoveTileBlockMapPointerSouth::
  1614. add MAP_BORDER * 2
  1615. ld b, a
  1616. ld a, [de]
  1617. add b
  1618. ld [de], a
  1619. ret nc
  1620. inc de
  1621. ld a, [de]
  1622. inc a
  1623. ld [de], a
  1624. ret
  1625. MoveTileBlockMapPointerNorth::
  1626. add MAP_BORDER * 2
  1627. ld b, a
  1628. ld a, [de]
  1629. sub b
  1630. ld [de], a
  1631. ret nc
  1632. inc de
  1633. ld a, [de]
  1634. dec a
  1635. ld [de], a
  1636. ret
  1637. ; the following 6 functions are used to tell the V-blank handler to redraw
  1638. ; the portion of the map that was newly exposed due to the player's movement
  1639. ScheduleNorthRowRedraw::
  1640. coord hl, 0, 0
  1641. call CopyToRedrawRowOrColumnSrcTiles
  1642. ld a, [wMapViewVRAMPointer]
  1643. ld [hRedrawRowOrColumnDest], a
  1644. ld a, [wMapViewVRAMPointer + 1]
  1645. ld [hRedrawRowOrColumnDest + 1], a
  1646. ld a, REDRAW_ROW
  1647. ld [hRedrawRowOrColumnMode], a
  1648. ret
  1649. CopyToRedrawRowOrColumnSrcTiles::
  1650. ld de, wRedrawRowOrColumnSrcTiles
  1651. ld c, 2 * SCREEN_WIDTH
  1652. .loop
  1653. ld a, [hli]
  1654. ld [de], a
  1655. inc de
  1656. dec c
  1657. jr nz, .loop
  1658. ret
  1659. ScheduleSouthRowRedraw::
  1660. coord hl, 0, 16
  1661. call CopyToRedrawRowOrColumnSrcTiles
  1662. ld a, [wMapViewVRAMPointer]
  1663. ld l, a
  1664. ld a, [wMapViewVRAMPointer + 1]
  1665. ld h, a
  1666. ld bc, $0200
  1667. add hl, bc
  1668. ld a, h
  1669. and $03
  1670. or $98
  1671. ld [hRedrawRowOrColumnDest + 1], a
  1672. ld a, l
  1673. ld [hRedrawRowOrColumnDest], a
  1674. ld a, REDRAW_ROW
  1675. ld [hRedrawRowOrColumnMode], a
  1676. ret
  1677. ScheduleEastColumnRedraw::
  1678. coord hl, 18, 0
  1679. call ScheduleColumnRedrawHelper
  1680. ld a, [wMapViewVRAMPointer]
  1681. ld c, a
  1682. and $e0
  1683. ld b, a
  1684. ld a, c
  1685. add 18
  1686. and $1f
  1687. or b
  1688. ld [hRedrawRowOrColumnDest], a
  1689. ld a, [wMapViewVRAMPointer + 1]
  1690. ld [hRedrawRowOrColumnDest + 1], a
  1691. ld a, REDRAW_COL
  1692. ld [hRedrawRowOrColumnMode], a
  1693. ret
  1694. ScheduleColumnRedrawHelper::
  1695. ld de, wRedrawRowOrColumnSrcTiles
  1696. ld c, SCREEN_HEIGHT
  1697. .loop
  1698. ld a, [hli]
  1699. ld [de], a
  1700. inc de
  1701. ld a, [hl]
  1702. ld [de], a
  1703. inc de
  1704. ld a, 19
  1705. add l
  1706. ld l, a
  1707. jr nc, .noCarry
  1708. inc h
  1709. .noCarry
  1710. dec c
  1711. jr nz, .loop
  1712. ret
  1713. ScheduleWestColumnRedraw::
  1714. coord hl, 0, 0
  1715. call ScheduleColumnRedrawHelper
  1716. ld a, [wMapViewVRAMPointer]
  1717. ld [hRedrawRowOrColumnDest], a
  1718. ld a, [wMapViewVRAMPointer + 1]
  1719. ld [hRedrawRowOrColumnDest + 1], a
  1720. ld a, REDRAW_COL
  1721. ld [hRedrawRowOrColumnMode], a
  1722. ret
  1723. ; function to write the tiles that make up a tile block to memory
  1724. ; Input: c = tile block ID, hl = destination address
  1725. DrawTileBlock::
  1726. push hl
  1727. ld a, [wTilesetBlocksPtr] ; pointer to tiles
  1728. ld l, a
  1729. ld a, [wTilesetBlocksPtr + 1]
  1730. ld h, a
  1731. ld a, c
  1732. swap a
  1733. ld b, a
  1734. and $f0
  1735. ld c, a
  1736. ld a, b
  1737. and $0f
  1738. ld b, a ; bc = tile block ID * 0x10
  1739. add hl, bc
  1740. ld d, h
  1741. ld e, l ; de = address of the tile block's tiles
  1742. pop hl
  1743. ld c, $04 ; 4 loop iterations
  1744. .loop ; each loop iteration, write 4 tile numbers
  1745. push bc
  1746. ld a, [de]
  1747. ld [hli], a
  1748. inc de
  1749. ld a, [de]
  1750. ld [hli], a
  1751. inc de
  1752. ld a, [de]
  1753. ld [hli], a
  1754. inc de
  1755. ld a, [de]
  1756. ld [hl], a
  1757. inc de
  1758. ld bc, $0015
  1759. add hl, bc
  1760. pop bc
  1761. dec c
  1762. jr nz, .loop
  1763. ret
  1764. ; function to update joypad state and simulate button presses
  1765. JoypadOverworld::
  1766. xor a
  1767. ld [wSpriteStateData1 + 3], a
  1768. ld [wSpriteStateData1 + 5], a
  1769. call RunMapScript
  1770. call Joypad
  1771. ld a, [wFlags_D733]
  1772. bit 3, a ; check if a trainer wants a challenge
  1773. jr nz, .notForcedDownwards
  1774. ld a, [wCurMap]
  1775. cp ROUTE_17 ; Cycling Road
  1776. jr nz, .notForcedDownwards
  1777. ld a, [hJoyHeld]
  1778. and D_DOWN | D_UP | D_LEFT | D_RIGHT | B_BUTTON | A_BUTTON
  1779. jr nz, .notForcedDownwards
  1780. ld a, D_DOWN
  1781. ld [hJoyHeld], a ; on the cycling road, if there isn't a trainer and the player isn't pressing buttons, simulate a down press
  1782. .notForcedDownwards
  1783. ld a, [wd730]
  1784. bit 7, a
  1785. ret z
  1786. ; if simulating button presses
  1787. ld a, [hJoyHeld]
  1788. ld b, a
  1789. ld a, [wOverrideSimulatedJoypadStatesMask] ; bit mask for button presses that override simulated ones
  1790. and b
  1791. ret nz ; return if the simulated button presses are overridden
  1792. ld hl, wSimulatedJoypadStatesIndex
  1793. dec [hl]
  1794. ld a, [hl]
  1795. cp $ff
  1796. jr z, .doneSimulating ; if the end of the simulated button presses has been reached
  1797. ld hl, wSimulatedJoypadStatesEnd
  1798. add l
  1799. ld l, a
  1800. jr nc, .noCarry
  1801. inc h
  1802. .noCarry
  1803. ld a, [hl]
  1804. ld [hJoyHeld], a ; store simulated button press in joypad state
  1805. and a
  1806. ret nz
  1807. ld [hJoyPressed], a
  1808. ld [hJoyReleased], a
  1809. ret
  1810. ; if done simulating button presses
  1811. .doneSimulating
  1812. xor a
  1813. ld [wWastedByteCD3A], a
  1814. ld [wSimulatedJoypadStatesIndex], a
  1815. ld [wSimulatedJoypadStatesEnd], a
  1816. ld [wJoyIgnore], a
  1817. ld [hJoyHeld], a
  1818. ld hl, wd736
  1819. ld a, [hl]
  1820. and $f8
  1821. ld [hl], a
  1822. ld hl, wd730
  1823. res 7, [hl]
  1824. ret
  1825. ; function to check the tile ahead to determine if the character should get on land or keep surfing
  1826. ; sets carry if there is a collision and clears carry otherwise
  1827. ; It seems that this function has a bug in it, but due to luck, it doesn't
  1828. ; show up. After detecting a sprite collision, it jumps to the code that
  1829. ; checks if the next tile is passable instead of just directly jumping to the
  1830. ; "collision detected" code. However, it doesn't store the next tile in c,
  1831. ; so the old value of c is used. 2429 is always called before this function,
  1832. ; and 2429 always sets c to 0xF0. There is no 0xF0 background tile, so it
  1833. ; is considered impassable and it is detected as a collision.
  1834. CollisionCheckOnWater::
  1835. ld a, [wd730]
  1836. bit 7, a
  1837. jp nz, .noCollision ; return and clear carry if button presses are being simulated
  1838. ld a, [wPlayerDirection] ; the direction that the player is trying to go in
  1839. ld d, a
  1840. ld a, [wSpriteStateData1 + 12] ; the player sprite's collision data (bit field) (set in the sprite movement code)
  1841. and d ; check if a sprite is in the direction the player is trying to go
  1842. jr nz, .checkIfNextTileIsPassable ; bug?
  1843. ld hl, TilePairCollisionsWater
  1844. call CheckForJumpingAndTilePairCollisions
  1845. jr c, .collision
  1846. predef GetTileAndCoordsInFrontOfPlayer ; get tile in front of player (puts it in c and [wTileInFrontOfPlayer])
  1847. ld a, [wTileInFrontOfPlayer] ; tile in front of player
  1848. cp $14 ; water tile
  1849. jr z, .noCollision ; keep surfing if it's a water tile
  1850. cp $32 ; either the left tile of the S.S. Anne boarding platform or the tile on eastern coastlines (depending on the current tileset)
  1851. jr z, .checkIfVermilionDockTileset
  1852. cp $48 ; tile on right on coast lines in Safari Zone
  1853. jr z, .noCollision ; keep surfing
  1854. ; check if the [land] tile in front of the player is passable
  1855. .checkIfNextTileIsPassable
  1856. ld hl, wTilesetCollisionPtr ; pointer to list of passable tiles
  1857. ld a, [hli]
  1858. ld h, [hl]
  1859. ld l, a
  1860. .loop
  1861. ld a, [hli]
  1862. cp $ff
  1863. jr z, .collision
  1864. cp c
  1865. jr z, .stopSurfing ; stop surfing if the tile is passable
  1866. jr .loop
  1867. .collision
  1868. ld a, [wChannelSoundIDs + Ch4]
  1869. cp SFX_COLLISION ; check if collision sound is already playing
  1870. jr z, .setCarry
  1871. ld a, SFX_COLLISION
  1872. call PlaySound ; play collision sound (if it's not already playing)
  1873. .setCarry
  1874. scf
  1875. jr .done
  1876. .noCollision
  1877. and a
  1878. .done
  1879. ret
  1880. .stopSurfing
  1881. xor a
  1882. ld [wWalkBikeSurfState], a
  1883. call LoadPlayerSpriteGraphics
  1884. call PlayDefaultMusic
  1885. jr .noCollision
  1886. .checkIfVermilionDockTileset
  1887. ld a, [wCurMapTileset] ; tileset
  1888. cp SHIP_PORT ; Vermilion Dock tileset
  1889. jr nz, .noCollision ; keep surfing if it's not the boarding platform tile
  1890. jr .stopSurfing ; if it is the boarding platform tile, stop surfing
  1891. ; function to run the current map's script
  1892. RunMapScript::
  1893. push hl
  1894. push de
  1895. push bc
  1896. callba TryPushingBoulder
  1897. ld a, [wFlags_0xcd60]
  1898. bit 1, a ; play boulder dust animation
  1899. jr z, .afterBoulderEffect
  1900. callba DoBoulderDustAnimation
  1901. .afterBoulderEffect
  1902. pop bc
  1903. pop de
  1904. pop hl
  1905. call RunNPCMovementScript
  1906. ld a, [wCurMap] ; current map number
  1907. call SwitchToMapRomBank ; change to the ROM bank the map's data is in
  1908. ld hl, wMapScriptPtr
  1909. ld a, [hli]
  1910. ld h, [hl]
  1911. ld l, a
  1912. ld de, .return
  1913. push de
  1914. jp hl ; jump to script
  1915. .return
  1916. ret
  1917. LoadWalkingPlayerSpriteGraphics::
  1918. ld de, RedSprite
  1919. ld hl, vNPCSprites
  1920. jr LoadPlayerSpriteGraphicsCommon
  1921. LoadSurfingPlayerSpriteGraphics::
  1922. ld de, SeelSprite
  1923. ld hl, vNPCSprites
  1924. jr LoadPlayerSpriteGraphicsCommon
  1925. LoadBikePlayerSpriteGraphics::
  1926. ld de, RedCyclingSprite
  1927. ld hl, vNPCSprites
  1928. LoadPlayerSpriteGraphicsCommon::
  1929. push de
  1930. push hl
  1931. lb bc, BANK(RedSprite), $0c
  1932. call CopyVideoData
  1933. pop hl
  1934. pop de
  1935. ld a, $c0
  1936. add e
  1937. ld e, a
  1938. jr nc, .noCarry
  1939. inc d
  1940. .noCarry
  1941. set 3, h
  1942. lb bc, BANK(RedSprite), $0c
  1943. jp CopyVideoData
  1944. ; function to load data from the map header
  1945. LoadMapHeader::
  1946. callba MarkTownVisitedAndLoadMissableObjects
  1947. ld a, [wCurMapTileset]
  1948. ld [wUnusedD119], a
  1949. ld a, [wCurMap]
  1950. call SwitchToMapRomBank
  1951. ld a, [wCurMapTileset]
  1952. ld b, a
  1953. res 7, a
  1954. ld [wCurMapTileset], a
  1955. ld [hPreviousTileset], a
  1956. bit 7, b
  1957. ret nz
  1958. ld hl, MapHeaderPointers
  1959. ld a, [wCurMap]
  1960. sla a
  1961. jr nc, .noCarry1
  1962. inc h
  1963. .noCarry1
  1964. add l
  1965. ld l, a
  1966. jr nc, .noCarry2
  1967. inc h
  1968. .noCarry2
  1969. ld a, [hli]
  1970. ld h, [hl]
  1971. ld l, a ; hl = base of map header
  1972. ; copy the first 10 bytes (the fixed area) of the map data to D367-D370
  1973. ld de, wCurMapTileset
  1974. ld c, $0a
  1975. .copyFixedHeaderLoop
  1976. ld a, [hli]
  1977. ld [de], a
  1978. inc de
  1979. dec c
  1980. jr nz, .copyFixedHeaderLoop
  1981. ; initialize all the connected maps to disabled at first, before loading the actual values
  1982. ld a, $ff
  1983. ld [wMapConn1Ptr], a
  1984. ld [wMapConn2Ptr], a
  1985. ld [wMapConn3Ptr], a
  1986. ld [wMapConn4Ptr], a
  1987. ; copy connection data (if any) to WRAM
  1988. ld a, [wMapConnections]
  1989. ld b, a
  1990. .checkNorth
  1991. bit 3, b
  1992. jr z, .checkSouth
  1993. ld de, wMapConn1Ptr
  1994. call CopyMapConnectionHeader
  1995. .checkSouth
  1996. bit 2, b
  1997. jr z, .checkWest
  1998. ld de, wMapConn2Ptr
  1999. call CopyMapConnectionHeader
  2000. .checkWest
  2001. bit 1, b
  2002. jr z, .checkEast
  2003. ld de, wMapConn3Ptr
  2004. call CopyMapConnectionHeader
  2005. .checkEast
  2006. bit 0, b
  2007. jr z, .getObjectDataPointer
  2008. ld de, wMapConn4Ptr
  2009. call CopyMapConnectionHeader
  2010. .getObjectDataPointer
  2011. ld a, [hli]
  2012. ld [wObjectDataPointerTemp], a
  2013. ld a, [hli]
  2014. ld [wObjectDataPointerTemp + 1], a
  2015. push hl
  2016. ld a, [wObjectDataPointerTemp]
  2017. ld l, a
  2018. ld a, [wObjectDataPointerTemp + 1]
  2019. ld h, a ; hl = base of object data
  2020. ld de, wMapBackgroundTile
  2021. ld a, [hli]
  2022. ld [de], a
  2023. .loadWarpData
  2024. ld a, [hli]
  2025. ld [wNumberOfWarps], a
  2026. and a
  2027. jr z, .loadSignData
  2028. ld c, a
  2029. ld de, wWarpEntries
  2030. .warpLoop ; one warp per loop iteration
  2031. ld b, $04
  2032. .warpInnerLoop
  2033. ld a, [hli]
  2034. ld [de], a
  2035. inc de
  2036. dec b
  2037. jr nz, .warpInnerLoop
  2038. dec c
  2039. jr nz, .warpLoop
  2040. .loadSignData
  2041. ld a, [hli] ; number of signs
  2042. ld [wNumSigns], a
  2043. and a ; are there any signs?
  2044. jr z, .loadSpriteData ; if not, skip this
  2045. ld c, a
  2046. ld de, wSignTextIDs
  2047. ld a, d
  2048. ld [hSignCoordPointer], a
  2049. ld a, e
  2050. ld [hSignCoordPointer + 1], a
  2051. ld de, wSignCoords
  2052. .signLoop
  2053. ld a, [hli]
  2054. ld [de], a
  2055. inc de
  2056. ld a, [hli]
  2057. ld [de], a
  2058. inc de
  2059. push de
  2060. ld a, [hSignCoordPointer]
  2061. ld d, a
  2062. ld a, [hSignCoordPointer + 1]
  2063. ld e, a
  2064. ld a, [hli]
  2065. ld [de], a
  2066. inc de
  2067. ld a, d
  2068. ld [hSignCoordPointer], a
  2069. ld a, e
  2070. ld [hSignCoordPointer + 1], a
  2071. pop de
  2072. dec c
  2073. jr nz, .signLoop
  2074. .loadSpriteData
  2075. ld a, [wd72e]
  2076. bit 5, a ; did a battle happen immediately before this?
  2077. jp nz, .finishUp ; if so, skip this because battles don't destroy this data
  2078. ld a, [hli]
  2079. ld [wNumSprites], a ; save the number of sprites
  2080. push hl
  2081. ; zero C110-C1FF and C210-C2FF
  2082. ld hl, wSpriteStateData1 + $10
  2083. ld de, wSpriteStateData2 + $10
  2084. xor a
  2085. ld b, $f0
  2086. .zeroSpriteDataLoop
  2087. ld [hli], a
  2088. ld [de], a
  2089. inc e
  2090. dec b
  2091. jr nz, .zeroSpriteDataLoop
  2092. ; initialize all C100-C1FF sprite entries to disabled (other than player's)
  2093. ld hl, wSpriteStateData1 + $12
  2094. ld de, $0010
  2095. ld c, $0f
  2096. .disableSpriteEntriesLoop
  2097. ld [hl], $ff
  2098. add hl, de
  2099. dec c
  2100. jr nz, .disableSpriteEntriesLoop
  2101. pop hl
  2102. ld de, wSpriteStateData1 + $10
  2103. ld a, [wNumSprites] ; number of sprites
  2104. and a ; are there any sprites?
  2105. jp z, .finishUp ; if there are no sprites, skip the rest
  2106. ld b, a
  2107. ld c, $00
  2108. .loadSpriteLoop
  2109. ld a, [hli]
  2110. ld [de], a ; store picture ID at C1X0
  2111. inc d
  2112. ld a, $04
  2113. add e
  2114. ld e, a
  2115. ld a, [hli]
  2116. ld [de], a ; store Y position at C2X4
  2117. inc e
  2118. ld a, [hli]
  2119. ld [de], a ; store X position at C2X5
  2120. inc e
  2121. ld a, [hli]
  2122. ld [de], a ; store movement byte 1 at C2X6
  2123. ld a, [hli]
  2124. ld [hLoadSpriteTemp1], a ; save movement byte 2
  2125. ld a, [hli]
  2126. ld [hLoadSpriteTemp2], a ; save text ID and flags byte
  2127. push bc
  2128. push hl
  2129. ld b, $00
  2130. ld hl, wMapSpriteData
  2131. add hl, bc
  2132. ld a, [hLoadSpriteTemp1]
  2133. ld [hli], a ; store movement byte 2 in byte 0 of sprite entry
  2134. ld a, [hLoadSpriteTemp2]
  2135. ld [hl], a ; this appears pointless, since the value is overwritten immediately after
  2136. ld a, [hLoadSpriteTemp2]
  2137. ld [hLoadSpriteTemp1], a
  2138. and $3f
  2139. ld [hl], a ; store text ID in byte 1 of sprite entry
  2140. pop hl
  2141. ld a, [hLoadSpriteTemp1]
  2142. bit 6, a
  2143. jr nz, .trainerSprite
  2144. bit 7, a
  2145. jr nz, .itemBallSprite
  2146. jr .regularSprite
  2147. .trainerSprite
  2148. ld a, [hli]
  2149. ld [hLoadSpriteTemp1], a ; save trainer class
  2150. ld a, [hli]
  2151. ld [hLoadSpriteTemp2], a ; save trainer number (within class)
  2152. push hl
  2153. ld hl, wMapSpriteExtraData
  2154. add hl, bc
  2155. ld a, [hLoadSpriteTemp1]
  2156. ld [hli], a ; store trainer class in byte 0 of the entry
  2157. ld a, [hLoadSpriteTemp2]
  2158. ld [hl], a ; store trainer number in byte 1 of the entry
  2159. pop hl
  2160. jr .nextSprite
  2161. .itemBallSprite
  2162. ld a, [hli]
  2163. ld [hLoadSpriteTemp1], a ; save item number
  2164. push hl
  2165. ld hl, wMapSpriteExtraData
  2166. add hl, bc
  2167. ld a, [hLoadSpriteTemp1]
  2168. ld [hli], a ; store item number in byte 0 of the entry
  2169. xor a
  2170. ld [hl], a ; zero byte 1, since it is not used
  2171. pop hl
  2172. jr .nextSprite
  2173. .regularSprite
  2174. push hl
  2175. ld hl, wMapSpriteExtraData
  2176. add hl, bc
  2177. ; zero both bytes, since regular sprites don't use this extra space
  2178. xor a
  2179. ld [hli], a
  2180. ld [hl], a
  2181. pop hl
  2182. .nextSprite
  2183. pop bc
  2184. dec d
  2185. ld a, $0a
  2186. add e
  2187. ld e, a
  2188. inc c
  2189. inc c
  2190. dec b
  2191. jp nz, .loadSpriteLoop
  2192. .finishUp
  2193. predef LoadTilesetHeader
  2194. callab LoadWildData
  2195. pop hl ; restore hl from before going to the warp/sign/sprite data (this value was saved for seemingly no purpose)
  2196. ld a, [wCurMapHeight] ; map height in 4x4 tile blocks
  2197. add a ; double it
  2198. ld [wCurrentMapHeight2], a ; store map height in 2x2 tile blocks
  2199. ld a, [wCurMapWidth] ; map width in 4x4 tile blocks
  2200. add a ; double it
  2201. ld [wCurrentMapWidth2], a ; map width in 2x2 tile blocks
  2202. ld a, [wCurMap]
  2203. ld c, a
  2204. ld b, $00
  2205. ld a, [H_LOADEDROMBANK]
  2206. push af
  2207. ld a, BANK(MapSongBanks)
  2208. ld [H_LOADEDROMBANK], a
  2209. ld [MBC1RomBank], a
  2210. ld hl, MapSongBanks
  2211. add hl, bc
  2212. add hl, bc
  2213. ld a, [hli]
  2214. ld [wMapMusicSoundID], a ; music 1
  2215. ld a, [hl]
  2216. ld [wMapMusicROMBank], a ; music 2
  2217. pop af
  2218. ld [H_LOADEDROMBANK], a
  2219. ld [MBC1RomBank], a
  2220. ret
  2221. ; function to copy map connection data from ROM to WRAM
  2222. ; Input: hl = source, de = destination
  2223. CopyMapConnectionHeader::
  2224. ld c, $0b
  2225. .loop
  2226. ld a, [hli]
  2227. ld [de], a
  2228. inc de
  2229. dec c
  2230. jr nz, .loop
  2231. ret
  2232. ; function to load map data
  2233. LoadMapData::
  2234. ld a, [H_LOADEDROMBANK]
  2235. push af
  2236. call DisableLCD
  2237. ld a, $98
  2238. ld [wMapViewVRAMPointer + 1], a
  2239. xor a
  2240. ld [wMapViewVRAMPointer], a
  2241. ld [hSCY], a
  2242. ld [hSCX], a
  2243. ld [wWalkCounter], a
  2244. ld [wUnusedD119], a
  2245. ld [wWalkBikeSurfStateCopy], a
  2246. ld [wSpriteSetID], a
  2247. call LoadTextBoxTilePatterns
  2248. call LoadMapHeader
  2249. callba InitMapSprites ; load tile pattern data for sprites
  2250. call LoadTileBlockMap
  2251. call LoadTilesetTilePatternData
  2252. call LoadCurrentMapView
  2253. ; copy current map view to VRAM
  2254. coord hl, 0, 0
  2255. ld de, vBGMap0
  2256. ld b, 18
  2257. .vramCopyLoop
  2258. ld c, 20
  2259. .vramCopyInnerLoop
  2260. ld a, [hli]
  2261. ld [de], a
  2262. inc e
  2263. dec c
  2264. jr nz, .vramCopyInnerLoop
  2265. ld a, 32 - 20
  2266. add e
  2267. ld e, a
  2268. jr nc, .noCarry
  2269. inc d
  2270. .noCarry
  2271. dec b
  2272. jr nz, .vramCopyLoop
  2273. ld a, $01
  2274. ld [wUpdateSpritesEnabled], a
  2275. call EnableLCD
  2276. ld b, SET_PAL_OVERWORLD
  2277. call RunPaletteCommand
  2278. call LoadPlayerSpriteGraphics
  2279. ld a, [wd732]
  2280. and 1 << 4 | 1 << 3 ; fly warp or dungeon warp
  2281. jr nz, .restoreRomBank
  2282. ld a, [wFlags_D733]
  2283. bit 1, a
  2284. jr nz, .restoreRomBank
  2285. call UpdateMusic6Times
  2286. call PlayDefaultMusicFadeOutCurrent
  2287. .restoreRomBank
  2288. pop af
  2289. ld [H_LOADEDROMBANK], a
  2290. ld [MBC1RomBank], a
  2291. ret
  2292. ; function to switch to the ROM bank that a map is stored in
  2293. ; Input: a = map number
  2294. SwitchToMapRomBank::
  2295. push hl
  2296. push bc
  2297. ld c, a
  2298. ld b, $00
  2299. ld a, Bank(MapHeaderBanks)
  2300. call BankswitchHome ; switch to ROM bank 3
  2301. ld hl, MapHeaderBanks
  2302. add hl, bc
  2303. ld a, [hl]
  2304. ld [$ffe8], a ; save map ROM bank
  2305. call BankswitchBack
  2306. ld a, [$ffe8]
  2307. ld [H_LOADEDROMBANK], a
  2308. ld [MBC1RomBank], a ; switch to map ROM bank
  2309. pop bc
  2310. pop hl
  2311. ret
  2312. IgnoreInputForHalfSecond:
  2313. ld a, 30
  2314. ld [wIgnoreInputCounter], a
  2315. ld hl, wd730
  2316. ld a, [hl]
  2317. or %00100110
  2318. ld [hl], a ; set ignore input bit
  2319. ret
  2320. ResetUsingStrengthOutOfBattleBit:
  2321. ld hl, wd728
  2322. res 0, [hl]
  2323. ret
  2324. ForceBikeOrSurf::
  2325. ld b, BANK(RedSprite)
  2326. ld hl, LoadPlayerSpriteGraphics
  2327. call Bankswitch
  2328. jp PlayDefaultMusic ; update map/player state?