home.asm 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722
  1. ; The rst vectors are unused.
  2. SECTION "rst 00", ROM0 [$00]
  3. rst $38
  4. SECTION "rst 08", ROM0 [$08]
  5. rst $38
  6. SECTION "rst 10", ROM0 [$10]
  7. rst $38
  8. SECTION "rst 18", ROM0 [$18]
  9. rst $38
  10. SECTION "rst 20", ROM0 [$20]
  11. rst $38
  12. SECTION "rst 28", ROM0 [$28]
  13. rst $38
  14. SECTION "rst 30", ROM0 [$30]
  15. rst $38
  16. SECTION "rst 38", ROM0 [$38]
  17. rst $38
  18. ; Hardware interrupts
  19. SECTION "vblank", ROM0 [$40]
  20. jp VBlank
  21. SECTION "hblank", ROM0 [$48]
  22. rst $38
  23. SECTION "timer", ROM0 [$50]
  24. jp Timer
  25. SECTION "serial", ROM0 [$58]
  26. jp Serial
  27. SECTION "joypad", ROM0 [$60]
  28. reti
  29. SECTION "Home", ROM0
  30. DisableLCD::
  31. xor a
  32. ld [rIF], a
  33. ld a, [rIE]
  34. ld b, a
  35. res 0, a
  36. ld [rIE], a
  37. .wait
  38. ld a, [rLY]
  39. cp LY_VBLANK
  40. jr nz, .wait
  41. ld a, [rLCDC]
  42. and $ff ^ rLCDC_ENABLE_MASK
  43. ld [rLCDC], a
  44. ld a, b
  45. ld [rIE], a
  46. ret
  47. EnableLCD::
  48. ld a, [rLCDC]
  49. set rLCDC_ENABLE, a
  50. ld [rLCDC], a
  51. ret
  52. ClearSprites::
  53. xor a
  54. ld hl, wOAMBuffer
  55. ld b, 40 * 4
  56. .loop
  57. ld [hli], a
  58. dec b
  59. jr nz, .loop
  60. ret
  61. HideSprites::
  62. ld a, 160
  63. ld hl, wOAMBuffer
  64. ld de, 4
  65. ld b, 40
  66. .loop
  67. ld [hl], a
  68. add hl, de
  69. dec b
  70. jr nz, .loop
  71. ret
  72. INCLUDE "home/copy.asm"
  73. SECTION "Entry", ROM0 [$100]
  74. nop
  75. jp Start
  76. SECTION "Header", ROM0 [$104]
  77. ; The header is generated by rgbfix.
  78. ; The space here is allocated to prevent code from being overwritten.
  79. ds $150 - $104
  80. SECTION "Main", ROM0
  81. Start::
  82. cp GBC
  83. jr z, .gbc
  84. xor a
  85. jr .ok
  86. .gbc
  87. ld a, 0
  88. .ok
  89. ld [wGBC], a
  90. jp Init
  91. INCLUDE "home/joypad.asm"
  92. INCLUDE "data/map_header_pointers.asm"
  93. INCLUDE "home/overworld.asm"
  94. CheckForUserInterruption::
  95. ; Return carry if Up+Select+B, Start or A are pressed in c frames.
  96. ; Used only in the intro and title screen.
  97. call DelayFrame
  98. push bc
  99. call JoypadLowSensitivity
  100. pop bc
  101. ld a, [hJoyHeld]
  102. cp D_UP + SELECT + B_BUTTON
  103. jr z, .input
  104. ld a, [hJoy5]
  105. and START | A_BUTTON
  106. jr nz, .input
  107. dec c
  108. jr nz, CheckForUserInterruption
  109. and a
  110. ret
  111. .input
  112. scf
  113. ret
  114. ; function to load position data for destination warp when switching maps
  115. ; INPUT:
  116. ; a = ID of destination warp within destination map
  117. LoadDestinationWarpPosition::
  118. ld b, a
  119. ld a, [H_LOADEDROMBANK]
  120. push af
  121. ld a, [wPredefParentBank]
  122. ld [H_LOADEDROMBANK], a
  123. ld [MBC1RomBank], a
  124. ld a, b
  125. add a
  126. add a
  127. ld c, a
  128. ld b, 0
  129. add hl, bc
  130. ld bc, 4
  131. ld de, wCurrentTileBlockMapViewPointer
  132. call CopyData
  133. pop af
  134. ld [H_LOADEDROMBANK], a
  135. ld [MBC1RomBank], a
  136. ret
  137. DrawHPBar::
  138. ; Draw an HP bar d tiles long, and fill it to e pixels.
  139. ; If c is nonzero, show at least a sliver regardless.
  140. ; The right end of the bar changes with [wHPBarType].
  141. push hl
  142. push de
  143. push bc
  144. ; Left
  145. ld a, $71 ; "HP:"
  146. ld [hli], a
  147. ld a, $62
  148. ld [hli], a
  149. push hl
  150. ; Middle
  151. ld a, $63 ; empty
  152. .draw
  153. ld [hli], a
  154. dec d
  155. jr nz, .draw
  156. ; Right
  157. ld a, [wHPBarType]
  158. dec a
  159. ld a, $6d ; status screen and battle
  160. jr z, .ok
  161. dec a ; pokemon menu
  162. .ok
  163. ld [hl], a
  164. pop hl
  165. ld a, e
  166. and a
  167. jr nz, .fill
  168. ; If c is nonzero, draw a pixel anyway.
  169. ld a, c
  170. and a
  171. jr z, .done
  172. ld e, 1
  173. .fill
  174. ld a, e
  175. sub 8
  176. jr c, .partial
  177. ld e, a
  178. ld a, $6b ; full
  179. ld [hli], a
  180. ld a, e
  181. and a
  182. jr z, .done
  183. jr .fill
  184. .partial
  185. ; Fill remaining pixels at the end if necessary.
  186. ld a, $63 ; empty
  187. add e
  188. ld [hl], a
  189. .done
  190. pop bc
  191. pop de
  192. pop hl
  193. ret
  194. ; loads pokemon data from one of multiple sources to wLoadedMon
  195. ; loads base stats to wMonHeader
  196. ; INPUT:
  197. ; [wWhichPokemon] = index of pokemon within party/box
  198. ; [wMonDataLocation] = source
  199. ; 00: player's party
  200. ; 01: enemy's party
  201. ; 02: current box
  202. ; 03: daycare
  203. ; OUTPUT:
  204. ; [wcf91] = pokemon ID
  205. ; wLoadedMon = base address of pokemon data
  206. ; wMonHeader = base address of base stats
  207. LoadMonData::
  208. jpab LoadMonData_
  209. OverwritewMoves::
  210. ; Write c to [wMoves + b]. Unused.
  211. ld hl, wMoves
  212. ld e, b
  213. ld d, 0
  214. add hl, de
  215. ld a, c
  216. ld [hl], a
  217. ret
  218. LoadFlippedFrontSpriteByMonIndex::
  219. ld a, 1
  220. ld [wSpriteFlipped], a
  221. LoadFrontSpriteByMonIndex::
  222. push hl
  223. ld a, [wd11e]
  224. push af
  225. ld a, [wcf91]
  226. ld [wd11e], a
  227. predef IndexToPokedex
  228. ld hl, wd11e
  229. ld a, [hl]
  230. pop bc
  231. ld [hl], b
  232. and a
  233. pop hl
  234. jr z, .invalidDexNumber ; dex #0 invalid
  235. cp NUM_POKEMON + 1
  236. jr c, .validDexNumber ; dex >#151 invalid
  237. .invalidDexNumber
  238. ld a, RHYDON ; $1
  239. ld [wcf91], a
  240. ret
  241. .validDexNumber
  242. push hl
  243. ld de, vFrontPic
  244. call LoadMonFrontSprite
  245. pop hl
  246. ld a, [H_LOADEDROMBANK]
  247. push af
  248. ld a, Bank(CopyUncompressedPicToHL)
  249. ld [H_LOADEDROMBANK], a
  250. ld [MBC1RomBank], a
  251. xor a
  252. ld [hStartTileID], a
  253. call CopyUncompressedPicToHL
  254. xor a
  255. ld [wSpriteFlipped], a
  256. pop af
  257. ld [H_LOADEDROMBANK], a
  258. ld [MBC1RomBank], a
  259. ret
  260. PlayCry::
  261. ; Play monster a's cry.
  262. call GetCryData
  263. call PlaySound
  264. jp WaitForSoundToFinish
  265. GetCryData::
  266. ; Load cry data for monster a.
  267. dec a
  268. ld c, a
  269. ld b, 0
  270. ld hl, CryData
  271. add hl, bc
  272. add hl, bc
  273. add hl, bc
  274. ld a, BANK(CryData)
  275. call BankswitchHome
  276. ld a, [hli]
  277. ld b, a ; cry id
  278. ld a, [hli]
  279. ld [wFrequencyModifier], a
  280. ld a, [hl]
  281. ld [wTempoModifier], a
  282. call BankswitchBack
  283. ; Cry headers have 3 channels,
  284. ; and start from index $14,
  285. ; so add 3 times the cry id.
  286. ld a, b
  287. ld c, $14
  288. rlca ; * 2
  289. add b
  290. add c
  291. ret
  292. DisplayPartyMenu::
  293. ld a, [hTilesetType]
  294. push af
  295. xor a
  296. ld [hTilesetType], a
  297. call GBPalWhiteOutWithDelay3
  298. call ClearSprites
  299. call PartyMenuInit
  300. call DrawPartyMenu
  301. jp HandlePartyMenuInput
  302. GoBackToPartyMenu::
  303. ld a, [hTilesetType]
  304. push af
  305. xor a
  306. ld [hTilesetType], a
  307. call PartyMenuInit
  308. call RedrawPartyMenu
  309. jp HandlePartyMenuInput
  310. PartyMenuInit::
  311. ld a, 1 ; hardcoded bank
  312. call BankswitchHome
  313. call LoadHpBarAndStatusTilePatterns
  314. ld hl, wd730
  315. set 6, [hl] ; turn off letter printing delay
  316. xor a ; PLAYER_PARTY_DATA
  317. ld [wMonDataLocation], a
  318. ld [wMenuWatchMovingOutOfBounds], a
  319. ld hl, wTopMenuItemY
  320. inc a
  321. ld [hli], a ; top menu item Y
  322. xor a
  323. ld [hli], a ; top menu item X
  324. ld a, [wPartyAndBillsPCSavedMenuItem]
  325. push af
  326. ld [hli], a ; current menu item ID
  327. inc hl
  328. ld a, [wPartyCount]
  329. and a ; are there more than 0 pokemon in the party?
  330. jr z, .storeMaxMenuItemID
  331. dec a
  332. ; if party is not empty, the max menu item ID is ([wPartyCount] - 1)
  333. ; otherwise, it is 0
  334. .storeMaxMenuItemID
  335. ld [hli], a ; max menu item ID
  336. ld a, [wForcePlayerToChooseMon]
  337. and a
  338. ld a, A_BUTTON | B_BUTTON
  339. jr z, .next
  340. xor a
  341. ld [wForcePlayerToChooseMon], a
  342. inc a ; a = A_BUTTON
  343. .next
  344. ld [hli], a ; menu watched keys
  345. pop af
  346. ld [hl], a ; old menu item ID
  347. ret
  348. HandlePartyMenuInput::
  349. ld a, 1
  350. ld [wMenuWrappingEnabled], a
  351. ld a, $40
  352. ld [wPartyMenuAnimMonEnabled], a
  353. call HandleMenuInput_
  354. call PlaceUnfilledArrowMenuCursor
  355. ld b, a
  356. xor a
  357. ld [wPartyMenuAnimMonEnabled], a
  358. ld a, [wCurrentMenuItem]
  359. ld [wPartyAndBillsPCSavedMenuItem], a
  360. ld hl, wd730
  361. res 6, [hl] ; turn on letter printing delay
  362. ld a, [wMenuItemToSwap]
  363. and a
  364. jp nz, .swappingPokemon
  365. pop af
  366. ld [hTilesetType], a
  367. bit 1, b
  368. jr nz, .noPokemonChosen
  369. ld a, [wPartyCount]
  370. and a
  371. jr z, .noPokemonChosen
  372. ld a, [wCurrentMenuItem]
  373. ld [wWhichPokemon], a
  374. ld hl, wPartySpecies
  375. ld b, 0
  376. ld c, a
  377. add hl, bc
  378. ld a, [hl]
  379. ld [wcf91], a
  380. ld [wBattleMonSpecies2], a
  381. call BankswitchBack
  382. and a
  383. ret
  384. .noPokemonChosen
  385. call BankswitchBack
  386. scf
  387. ret
  388. .swappingPokemon
  389. bit 1, b ; was the B button pressed?
  390. jr z, .handleSwap ; if not, handle swapping the pokemon
  391. .cancelSwap ; if the B button was pressed
  392. callba ErasePartyMenuCursors
  393. xor a
  394. ld [wMenuItemToSwap], a
  395. ld [wPartyMenuTypeOrMessageID], a
  396. call RedrawPartyMenu
  397. jr HandlePartyMenuInput
  398. .handleSwap
  399. ld a, [wCurrentMenuItem]
  400. ld [wWhichPokemon], a
  401. callba SwitchPartyMon
  402. jr HandlePartyMenuInput
  403. DrawPartyMenu::
  404. ld hl, DrawPartyMenu_
  405. jr DrawPartyMenuCommon
  406. RedrawPartyMenu::
  407. ld hl, RedrawPartyMenu_
  408. DrawPartyMenuCommon::
  409. ld b, BANK(RedrawPartyMenu_)
  410. jp Bankswitch
  411. ; prints a pokemon's status condition
  412. ; INPUT:
  413. ; de = address of status condition
  414. ; hl = destination address
  415. PrintStatusCondition::
  416. push de
  417. dec de
  418. dec de ; de = address of current HP
  419. ld a, [de]
  420. ld b, a
  421. dec de
  422. ld a, [de]
  423. or b ; is the pokemon's HP zero?
  424. pop de
  425. jr nz, PrintStatusConditionNotFainted
  426. ; if the pokemon's HP is 0, print "FNT"
  427. ld a, "F"
  428. ld [hli], a
  429. ld a, "N"
  430. ld [hli], a
  431. ld [hl], "T"
  432. and a
  433. ret
  434. PrintStatusConditionNotFainted:
  435. ld a, [H_LOADEDROMBANK]
  436. push af
  437. ld a, BANK(PrintStatusAilment)
  438. ld [H_LOADEDROMBANK], a
  439. ld [MBC1RomBank], a
  440. call PrintStatusAilment ; print status condition
  441. pop bc
  442. ld a, b
  443. ld [H_LOADEDROMBANK], a
  444. ld [MBC1RomBank], a
  445. ret
  446. ; function to print pokemon level, leaving off the ":L" if the level is at least 100
  447. ; INPUT:
  448. ; hl = destination address
  449. ; [wLoadedMonLevel] = level
  450. PrintLevel::
  451. ld a, $6e ; ":L" tile ID
  452. ld [hli], a
  453. ld c, 2 ; number of digits
  454. ld a, [wLoadedMonLevel] ; level
  455. cp 100
  456. jr c, PrintLevelCommon
  457. ; if level at least 100, write over the ":L" tile
  458. dec hl
  459. inc c ; increment number of digits to 3
  460. jr PrintLevelCommon
  461. ; prints the level without leaving off ":L" regardless of level
  462. ; INPUT:
  463. ; hl = destination address
  464. ; [wLoadedMonLevel] = level
  465. PrintLevelFull::
  466. ld a, $6e ; ":L" tile ID
  467. ld [hli], a
  468. ld c, 3 ; number of digits
  469. ld a, [wLoadedMonLevel] ; level
  470. PrintLevelCommon::
  471. ld [wd11e], a
  472. ld de, wd11e
  473. ld b, LEFT_ALIGN | 1 ; 1 byte
  474. jp PrintNumber
  475. GetwMoves::
  476. ; Unused. Returns the move at index a from wMoves in a
  477. ld hl, wMoves
  478. ld c, a
  479. ld b, 0
  480. add hl, bc
  481. ld a, [hl]
  482. ret
  483. ; copies the base stat data of a pokemon to wMonHeader
  484. ; INPUT:
  485. ; [wd0b5] = pokemon ID
  486. GetMonHeader::
  487. ld a, [H_LOADEDROMBANK]
  488. push af
  489. ld a, BANK(BaseStats)
  490. ld [H_LOADEDROMBANK], a
  491. ld [MBC1RomBank], a
  492. push bc
  493. push de
  494. push hl
  495. ld a, [wd11e]
  496. push af
  497. ld a, [wd0b5]
  498. ld [wd11e], a
  499. ld de, FossilKabutopsPic
  500. ld b, $66 ; size of Kabutops fossil and Ghost sprites
  501. cp FOSSIL_KABUTOPS ; Kabutops fossil
  502. jr z, .specialID
  503. ld de, GhostPic
  504. cp MON_GHOST ; Ghost
  505. jr z, .specialID
  506. ld de, FossilAerodactylPic
  507. ld b, $77 ; size of Aerodactyl fossil sprite
  508. cp FOSSIL_AERODACTYL ; Aerodactyl fossil
  509. jr z, .specialID
  510. cp MEW
  511. jr z, .mew
  512. predef IndexToPokedex ; convert pokemon ID in [wd11e] to pokedex number
  513. ld a, [wd11e]
  514. dec a
  515. ld bc, MonBaseStatsEnd - MonBaseStats
  516. ld hl, BaseStats
  517. call AddNTimes
  518. ld de, wMonHeader
  519. ld bc, MonBaseStatsEnd - MonBaseStats
  520. call CopyData
  521. jr .done
  522. .specialID
  523. ld hl, wMonHSpriteDim
  524. ld [hl], b ; write sprite dimensions
  525. inc hl
  526. ld [hl], e ; write front sprite pointer
  527. inc hl
  528. ld [hl], d
  529. jr .done
  530. .mew
  531. ld hl, MewBaseStats
  532. ld de, wMonHeader
  533. ld bc, MonBaseStatsEnd - MonBaseStats
  534. ld a, BANK(MewBaseStats)
  535. call FarCopyData
  536. .done
  537. ld a, [wd0b5]
  538. ld [wMonHIndex], a
  539. pop af
  540. ld [wd11e], a
  541. pop hl
  542. pop de
  543. pop bc
  544. pop af
  545. ld [H_LOADEDROMBANK], a
  546. ld [MBC1RomBank], a
  547. ret
  548. ; copy party pokemon's name to wcd6d
  549. GetPartyMonName2::
  550. ld a, [wWhichPokemon] ; index within party
  551. ld hl, wPartyMonNicks
  552. ; this is called more often
  553. GetPartyMonName::
  554. push hl
  555. push bc
  556. call SkipFixedLengthTextEntries ; add NAME_LENGTH to hl, a times
  557. ld de, wcd6d
  558. push de
  559. ld bc, NAME_LENGTH
  560. call CopyData
  561. pop de
  562. pop bc
  563. pop hl
  564. ret
  565. ; function to print a BCD (Binary-coded decimal) number
  566. ; de = address of BCD number
  567. ; hl = destination address
  568. ; c = flags and length
  569. ; bit 7: if set, do not print leading zeroes
  570. ; if unset, print leading zeroes
  571. ; bit 6: if set, left-align the string (do not pad empty digits with spaces)
  572. ; if unset, right-align the string
  573. ; bit 5: if set, print currency symbol at the beginning of the string
  574. ; if unset, do not print the currency symbol
  575. ; bits 0-4: length of BCD number in bytes
  576. ; Note that bits 5 and 7 are modified during execution. The above reflects
  577. ; their meaning at the beginning of the functions's execution.
  578. PrintBCDNumber::
  579. ld b, c ; save flags in b
  580. res 7, c
  581. res 6, c
  582. res 5, c ; c now holds the length
  583. bit 5, b
  584. jr z, .loop
  585. bit 7, b
  586. jr nz, .loop
  587. ld [hl], "¥"
  588. inc hl
  589. .loop
  590. ld a, [de]
  591. swap a
  592. call PrintBCDDigit ; print upper digit
  593. ld a, [de]
  594. call PrintBCDDigit ; print lower digit
  595. inc de
  596. dec c
  597. jr nz, .loop
  598. bit 7, b ; were any non-zero digits printed?
  599. jr z, .done ; if so, we are done
  600. .numberEqualsZero ; if every digit of the BCD number is zero
  601. bit 6, b ; left or right alignment?
  602. jr nz, .skipRightAlignmentAdjustment
  603. dec hl ; if the string is right-aligned, it needs to be moved back one space
  604. .skipRightAlignmentAdjustment
  605. bit 5, b
  606. jr z, .skipCurrencySymbol
  607. ld [hl], "¥"
  608. inc hl
  609. .skipCurrencySymbol
  610. ld [hl], "0"
  611. call PrintLetterDelay
  612. inc hl
  613. .done
  614. ret
  615. PrintBCDDigit::
  616. and $f
  617. and a
  618. jr z, .zeroDigit
  619. .nonzeroDigit
  620. bit 7, b ; have any non-space characters been printed?
  621. jr z, .outputDigit
  622. ; if bit 7 is set, then no numbers have been printed yet
  623. bit 5, b ; print the currency symbol?
  624. jr z, .skipCurrencySymbol
  625. ld [hl], "¥"
  626. inc hl
  627. res 5, b
  628. .skipCurrencySymbol
  629. res 7, b ; unset 7 to indicate that a nonzero digit has been reached
  630. .outputDigit
  631. add "0"
  632. ld [hli], a
  633. jp PrintLetterDelay
  634. .zeroDigit
  635. bit 7, b ; either printing leading zeroes or already reached a nonzero digit?
  636. jr z, .outputDigit ; if so, print a zero digit
  637. bit 6, b ; left or right alignment?
  638. ret nz
  639. inc hl ; if right-aligned, "print" a space by advancing the pointer
  640. ret
  641. ; uncompresses the front or back sprite of the specified mon
  642. ; assumes the corresponding mon header is already loaded
  643. ; hl contains offset to sprite pointer ($b for front or $d for back)
  644. UncompressMonSprite::
  645. ld bc, wMonHeader
  646. add hl, bc
  647. ld a, [hli]
  648. ld [wSpriteInputPtr], a ; fetch sprite input pointer
  649. ld a, [hl]
  650. ld [wSpriteInputPtr+1], a
  651. ; define (by index number) the bank that a pokemon's image is in
  652. ; index = Mew, bank 1
  653. ; index = Kabutops fossil, bank $B
  654. ; index < $1F, bank 9
  655. ; $1F ≤ index < $4A, bank $A
  656. ; $4A ≤ index < $74, bank $B
  657. ; $74 ≤ index < $99, bank $C
  658. ; $99 ≤ index, bank $D
  659. ld a, [wcf91] ; XXX name for this ram location
  660. ld b, a
  661. cp MEW
  662. ld a, BANK(MewPicFront)
  663. jr z, .GotBank
  664. ld a, b
  665. cp FOSSIL_KABUTOPS
  666. ld a, BANK(FossilKabutopsPic)
  667. jr z, .GotBank
  668. ld a, b
  669. cp TANGELA + 1
  670. ld a, BANK(TangelaPicFront)
  671. jr c, .GotBank
  672. ld a, b
  673. cp MOLTRES + 1
  674. ld a, BANK(MoltresPicFront)
  675. jr c, .GotBank
  676. ld a, b
  677. cp BEEDRILL + 2
  678. ld a, BANK(BeedrillPicFront)
  679. jr c, .GotBank
  680. ld a, b
  681. cp STARMIE + 1
  682. ld a, BANK(StarmiePicFront)
  683. jr c, .GotBank
  684. ld a, BANK(VictreebelPicFront)
  685. .GotBank
  686. jp UncompressSpriteData
  687. ; de: destination location
  688. LoadMonFrontSprite::
  689. push de
  690. ld hl, wMonHFrontSprite - wMonHeader
  691. call UncompressMonSprite
  692. ld hl, wMonHSpriteDim
  693. ld a, [hli]
  694. ld c, a
  695. pop de
  696. ; fall through
  697. ; postprocesses uncompressed sprite chunks to a 2bpp sprite and loads it into video ram
  698. ; calculates alignment parameters to place both sprite chunks in the center of the 7*7 tile sprite buffers
  699. ; de: destination location
  700. ; a,c: sprite dimensions (in tiles of 8x8 each)
  701. LoadUncompressedSpriteData::
  702. push de
  703. and $f
  704. ld [H_SPRITEWIDTH], a ; each byte contains 8 pixels (in 1bpp), so tiles=bytes for width
  705. ld b, a
  706. ld a, $7
  707. sub b ; 7-w
  708. inc a ; 8-w
  709. srl a ; (8-w)/2 ; horizontal center (in tiles, rounded up)
  710. ld b, a
  711. add a
  712. add a
  713. add a
  714. sub b ; 7*((8-w)/2) ; skip for horizontal center (in tiles)
  715. ld [H_SPRITEOFFSET], a
  716. ld a, c
  717. swap a
  718. and $f
  719. ld b, a
  720. add a
  721. add a
  722. add a ; 8*tiles is height in bytes
  723. ld [H_SPRITEHEIGHT], a
  724. ld a, $7
  725. sub b ; 7-h ; skip for vertical center (in tiles, relative to current column)
  726. ld b, a
  727. ld a, [H_SPRITEOFFSET]
  728. add b ; 7*((8-w)/2) + 7-h ; combined overall offset (in tiles)
  729. add a
  730. add a
  731. add a ; 8*(7*((8-w)/2) + 7-h) ; combined overall offset (in bytes)
  732. ld [H_SPRITEOFFSET], a
  733. xor a
  734. ld [$4000], a
  735. ld hl, sSpriteBuffer0
  736. call ZeroSpriteBuffer ; zero buffer 0
  737. ld de, sSpriteBuffer1
  738. ld hl, sSpriteBuffer0
  739. call AlignSpriteDataCentered ; copy and align buffer 1 to 0 (containing the MSB of the 2bpp sprite)
  740. ld hl, sSpriteBuffer1
  741. call ZeroSpriteBuffer ; zero buffer 1
  742. ld de, sSpriteBuffer2
  743. ld hl, sSpriteBuffer1
  744. call AlignSpriteDataCentered ; copy and align buffer 2 to 1 (containing the LSB of the 2bpp sprite)
  745. pop de
  746. jp InterlaceMergeSpriteBuffers
  747. ; copies and aligns the sprite data properly inside the sprite buffer
  748. ; sprite buffers are 7*7 tiles in size, the loaded sprite is centered within this area
  749. AlignSpriteDataCentered::
  750. ld a, [H_SPRITEOFFSET]
  751. ld b, $0
  752. ld c, a
  753. add hl, bc
  754. ld a, [H_SPRITEWIDTH]
  755. .columnLoop
  756. push af
  757. push hl
  758. ld a, [H_SPRITEHEIGHT]
  759. ld c, a
  760. .columnInnerLoop
  761. ld a, [de]
  762. inc de
  763. ld [hli], a
  764. dec c
  765. jr nz, .columnInnerLoop
  766. pop hl
  767. ld bc, 7*8 ; 7 tiles
  768. add hl, bc ; advance one full column
  769. pop af
  770. dec a
  771. jr nz, .columnLoop
  772. ret
  773. ; fills the sprite buffer (pointed to in hl) with zeros
  774. ZeroSpriteBuffer::
  775. ld bc, SPRITEBUFFERSIZE
  776. .nextByteLoop
  777. xor a
  778. ld [hli], a
  779. dec bc
  780. ld a, b
  781. or c
  782. jr nz, .nextByteLoop
  783. ret
  784. ; combines the (7*7 tiles, 1bpp) sprite chunks in buffer 0 and 1 into a 2bpp sprite located in buffer 1 through 2
  785. ; in the resulting sprite, the rows of the two source sprites are interlaced
  786. ; de: output address
  787. InterlaceMergeSpriteBuffers::
  788. xor a
  789. ld [$4000], a
  790. push de
  791. ld hl, sSpriteBuffer2 + (SPRITEBUFFERSIZE - 1) ; destination: end of buffer 2
  792. ld de, sSpriteBuffer1 + (SPRITEBUFFERSIZE - 1) ; source 2: end of buffer 1
  793. ld bc, sSpriteBuffer0 + (SPRITEBUFFERSIZE - 1) ; source 1: end of buffer 0
  794. ld a, SPRITEBUFFERSIZE/2 ; $c4
  795. ld [H_SPRITEINTERLACECOUNTER], a
  796. .interlaceLoop
  797. ld a, [de]
  798. dec de
  799. ld [hld], a ; write byte of source 2
  800. ld a, [bc]
  801. dec bc
  802. ld [hld], a ; write byte of source 1
  803. ld a, [de]
  804. dec de
  805. ld [hld], a ; write byte of source 2
  806. ld a, [bc]
  807. dec bc
  808. ld [hld], a ; write byte of source 1
  809. ld a, [H_SPRITEINTERLACECOUNTER]
  810. dec a
  811. ld [H_SPRITEINTERLACECOUNTER], a
  812. jr nz, .interlaceLoop
  813. ld a, [wSpriteFlipped]
  814. and a
  815. jr z, .notFlipped
  816. ld bc, 2*SPRITEBUFFERSIZE
  817. ld hl, sSpriteBuffer1
  818. .swapLoop
  819. swap [hl] ; if flipped swap nybbles in all bytes
  820. inc hl
  821. dec bc
  822. ld a, b
  823. or c
  824. jr nz, .swapLoop
  825. .notFlipped
  826. pop hl
  827. ld de, sSpriteBuffer1
  828. ld c, (2*SPRITEBUFFERSIZE)/16 ; $31, number of 16 byte chunks to be copied
  829. ld a, [H_LOADEDROMBANK]
  830. ld b, a
  831. jp CopyVideoData
  832. INCLUDE "data/collision.asm"
  833. INCLUDE "home/copy2.asm"
  834. INCLUDE "home/text.asm"
  835. INCLUDE "home/vcopy.asm"
  836. INCLUDE "home/init.asm"
  837. INCLUDE "home/vblank.asm"
  838. INCLUDE "home/fade.asm"
  839. INCLUDE "home/serial.asm"
  840. INCLUDE "home/timer.asm"
  841. INCLUDE "home/audio.asm"
  842. UpdateSprites::
  843. ld a, [wUpdateSpritesEnabled]
  844. dec a
  845. ret nz
  846. ld a, [H_LOADEDROMBANK]
  847. push af
  848. ld a, Bank(_UpdateSprites)
  849. ld [H_LOADEDROMBANK], a
  850. ld [MBC1RomBank], a
  851. call _UpdateSprites
  852. pop af
  853. ld [H_LOADEDROMBANK], a
  854. ld [MBC1RomBank], a
  855. ret
  856. INCLUDE "data/mart_inventories.asm"
  857. TextScriptEndingChar::
  858. db "@"
  859. TextScriptEnd::
  860. ld hl, TextScriptEndingChar
  861. ret
  862. ExclamationText::
  863. TX_FAR _ExclamationText
  864. db "@"
  865. GroundRoseText::
  866. TX_FAR _GroundRoseText
  867. db "@"
  868. BoulderText::
  869. TX_FAR _BoulderText
  870. db "@"
  871. MartSignText::
  872. TX_FAR _MartSignText
  873. db "@"
  874. PokeCenterSignText::
  875. TX_FAR _PokeCenterSignText
  876. db "@"
  877. PickUpItemText::
  878. TX_ASM
  879. predef PickUpItem
  880. jp TextScriptEnd
  881. INCLUDE "home/pic.asm"
  882. ResetPlayerSpriteData::
  883. ld hl, wSpriteStateData1
  884. call ResetPlayerSpriteData_ClearSpriteData
  885. ld hl, wSpriteStateData2
  886. call ResetPlayerSpriteData_ClearSpriteData
  887. ld a, $1
  888. ld [wSpriteStateData1], a
  889. ld [wSpriteStateData2 + $0e], a
  890. ld hl, wSpriteStateData1 + 4
  891. ld [hl], $3c ; set Y screen pos
  892. inc hl
  893. inc hl
  894. ld [hl], $40 ; set X screen pos
  895. ret
  896. ; overwrites sprite data with zeroes
  897. ResetPlayerSpriteData_ClearSpriteData::
  898. ld bc, $10
  899. xor a
  900. jp FillMemory
  901. FadeOutAudio::
  902. ld a, [wAudioFadeOutControl]
  903. and a ; currently fading out audio?
  904. jr nz, .fadingOut
  905. ld a, [wd72c]
  906. bit 1, a
  907. ret nz
  908. ld a, $77
  909. ld [rNR50], a
  910. ret
  911. .fadingOut
  912. ld a, [wAudioFadeOutCounter]
  913. and a
  914. jr z, .counterReachedZero
  915. dec a
  916. ld [wAudioFadeOutCounter], a
  917. ret
  918. .counterReachedZero
  919. ld a, [wAudioFadeOutCounterReloadValue]
  920. ld [wAudioFadeOutCounter], a
  921. ld a, [rNR50]
  922. and a ; has the volume reached 0?
  923. jr z, .fadeOutComplete
  924. ld b, a
  925. and $f
  926. dec a
  927. ld c, a
  928. ld a, b
  929. and $f0
  930. swap a
  931. dec a
  932. swap a
  933. or c
  934. ld [rNR50], a
  935. ret
  936. .fadeOutComplete
  937. ld a, [wAudioFadeOutControl]
  938. ld b, a
  939. xor a
  940. ld [wAudioFadeOutControl], a
  941. ld a, $ff
  942. ld [wNewSoundID], a
  943. call PlaySound
  944. ld a, [wAudioSavedROMBank]
  945. ld [wAudioROMBank], a
  946. ld a, b
  947. ld [wNewSoundID], a
  948. jp PlaySound
  949. ; this function is used to display sign messages, sprite dialog, etc.
  950. ; INPUT: [hSpriteIndexOrTextID] = sprite ID or text ID
  951. DisplayTextID::
  952. ld a, [H_LOADEDROMBANK]
  953. push af
  954. callba DisplayTextIDInit ; initialization
  955. ld hl, wTextPredefFlag
  956. bit 0, [hl]
  957. res 0, [hl]
  958. jr nz, .skipSwitchToMapBank
  959. ld a, [wCurMap]
  960. call SwitchToMapRomBank
  961. .skipSwitchToMapBank
  962. ld a, 30 ; half a second
  963. ld [H_FRAMECOUNTER], a ; used as joypad poll timer
  964. ld hl, wMapTextPtr
  965. ld a, [hli]
  966. ld h, [hl]
  967. ld l, a ; hl = map text pointer
  968. ld d, $00
  969. ld a, [hSpriteIndexOrTextID] ; text ID
  970. ld [wSpriteIndex], a
  971. and a
  972. jp z, DisplayStartMenu
  973. cp TEXT_SAFARI_GAME_OVER
  974. jp z, DisplaySafariGameOverText
  975. cp TEXT_MON_FAINTED
  976. jp z, DisplayPokemonFaintedText
  977. cp TEXT_BLACKED_OUT
  978. jp z, DisplayPlayerBlackedOutText
  979. cp TEXT_REPEL_WORE_OFF
  980. jp z, DisplayRepelWoreOffText
  981. ld a, [wNumSprites]
  982. ld e, a
  983. ld a, [hSpriteIndexOrTextID] ; sprite ID
  984. cp e
  985. jr z, .spriteHandling
  986. jr nc, .skipSpriteHandling
  987. .spriteHandling
  988. ; get the text ID of the sprite
  989. push hl
  990. push de
  991. push bc
  992. callba UpdateSpriteFacingOffsetAndDelayMovement ; update the graphics of the sprite the player is talking to (to face the right direction)
  993. pop bc
  994. pop de
  995. ld hl, wMapSpriteData ; NPC text entries
  996. ld a, [hSpriteIndexOrTextID]
  997. dec a
  998. add a
  999. add l
  1000. ld l, a
  1001. jr nc, .noCarry
  1002. inc h
  1003. .noCarry
  1004. inc hl
  1005. ld a, [hl] ; a = text ID of the sprite
  1006. pop hl
  1007. .skipSpriteHandling
  1008. ; look up the address of the text in the map's text entries
  1009. dec a
  1010. ld e, a
  1011. sla e
  1012. add hl, de
  1013. ld a, [hli]
  1014. ld h, [hl]
  1015. ld l, a ; hl = address of the text
  1016. ld a, [hl] ; a = first byte of text
  1017. ; check first byte of text for special cases
  1018. cp $fe ; Pokemart NPC
  1019. jp z, DisplayPokemartDialogue
  1020. cp $ff ; Pokemon Center NPC
  1021. jp z, DisplayPokemonCenterDialogue
  1022. cp $fc ; Item Storage PC
  1023. jp z, FuncTX_ItemStoragePC
  1024. cp $fd ; Bill's PC
  1025. jp z, FuncTX_BillsPC
  1026. cp $f9 ; Pokemon Center PC
  1027. jp z, FuncTX_PokemonCenterPC
  1028. cp $f5 ; Vending Machine
  1029. jr nz, .notVendingMachine
  1030. callba VendingMachineMenu ; jump banks to vending machine routine
  1031. jr AfterDisplayingTextID
  1032. .notVendingMachine
  1033. cp $f7 ; prize menu
  1034. jp z, FuncTX_GameCornerPrizeMenu
  1035. cp $f6 ; cable connection NPC in Pokemon Center
  1036. jr nz, .notSpecialCase
  1037. callab CableClubNPC
  1038. jr AfterDisplayingTextID
  1039. .notSpecialCase
  1040. call PrintText_NoCreatingTextBox ; display the text
  1041. ld a, [wDoNotWaitForButtonPressAfterDisplayingText]
  1042. and a
  1043. jr nz, HoldTextDisplayOpen
  1044. AfterDisplayingTextID::
  1045. ld a, [wEnteringCableClub]
  1046. and a
  1047. jr nz, HoldTextDisplayOpen
  1048. call WaitForTextScrollButtonPress ; wait for a button press after displaying all the text
  1049. ; loop to hold the dialogue box open as long as the player keeps holding down the A button
  1050. HoldTextDisplayOpen::
  1051. call Joypad
  1052. ld a, [hJoyHeld]
  1053. bit 0, a ; is the A button being pressed?
  1054. jr nz, HoldTextDisplayOpen
  1055. CloseTextDisplay::
  1056. ld a, [wCurMap]
  1057. call SwitchToMapRomBank
  1058. ld a, $90
  1059. ld [hWY], a ; move the window off the screen
  1060. call DelayFrame
  1061. call LoadGBPal
  1062. xor a
  1063. ld [H_AUTOBGTRANSFERENABLED], a ; disable continuous WRAM to VRAM transfer each V-blank
  1064. ; loop to make sprites face the directions they originally faced before the dialogue
  1065. ld hl, wSpriteStateData2 + $19
  1066. ld c, $0f
  1067. ld de, $0010
  1068. .restoreSpriteFacingDirectionLoop
  1069. ld a, [hl]
  1070. dec h
  1071. ld [hl], a
  1072. inc h
  1073. add hl, de
  1074. dec c
  1075. jr nz, .restoreSpriteFacingDirectionLoop
  1076. ld a, BANK(InitMapSprites)
  1077. ld [H_LOADEDROMBANK], a
  1078. ld [MBC1RomBank], a
  1079. call InitMapSprites ; reload sprite tile pattern data (since it was partially overwritten by text tile patterns)
  1080. ld hl, wFontLoaded
  1081. res 0, [hl]
  1082. ld a, [wd732]
  1083. bit 3, a ; used fly warp
  1084. call z, LoadPlayerSpriteGraphics
  1085. call LoadCurrentMapView
  1086. pop af
  1087. ld [H_LOADEDROMBANK], a
  1088. ld [MBC1RomBank], a
  1089. jp UpdateSprites
  1090. DisplayPokemartDialogue::
  1091. push hl
  1092. ld hl, PokemartGreetingText
  1093. call PrintText
  1094. pop hl
  1095. inc hl
  1096. call LoadItemList
  1097. ld a, PRICEDITEMLISTMENU
  1098. ld [wListMenuID], a
  1099. ld a, [H_LOADEDROMBANK]
  1100. push af
  1101. ld a, Bank(DisplayPokemartDialogue_)
  1102. ld [H_LOADEDROMBANK], a
  1103. ld [MBC1RomBank], a
  1104. call DisplayPokemartDialogue_
  1105. pop af
  1106. ld [H_LOADEDROMBANK], a
  1107. ld [MBC1RomBank], a
  1108. jp AfterDisplayingTextID
  1109. PokemartGreetingText::
  1110. TX_FAR _PokemartGreetingText
  1111. db "@"
  1112. LoadItemList::
  1113. ld a, 1
  1114. ld [wUpdateSpritesEnabled], a
  1115. ld a, h
  1116. ld [wItemListPointer], a
  1117. ld a, l
  1118. ld [wItemListPointer + 1], a
  1119. ld de, wItemList
  1120. .loop
  1121. ld a, [hli]
  1122. ld [de], a
  1123. inc de
  1124. cp $ff
  1125. jr nz, .loop
  1126. ret
  1127. DisplayPokemonCenterDialogue::
  1128. ; zeroing these doesn't appear to serve any purpose
  1129. xor a
  1130. ld [$ff8b], a
  1131. ld [$ff8c], a
  1132. ld [$ff8d], a
  1133. inc hl
  1134. ld a, [H_LOADEDROMBANK]
  1135. push af
  1136. ld a, Bank(DisplayPokemonCenterDialogue_)
  1137. ld [H_LOADEDROMBANK], a
  1138. ld [MBC1RomBank], a
  1139. call DisplayPokemonCenterDialogue_
  1140. pop af
  1141. ld [H_LOADEDROMBANK], a
  1142. ld [MBC1RomBank], a
  1143. jp AfterDisplayingTextID
  1144. DisplaySafariGameOverText::
  1145. callab PrintSafariGameOverText
  1146. jp AfterDisplayingTextID
  1147. DisplayPokemonFaintedText::
  1148. ld hl, PokemonFaintedText
  1149. call PrintText
  1150. jp AfterDisplayingTextID
  1151. PokemonFaintedText::
  1152. TX_FAR _PokemonFaintedText
  1153. db "@"
  1154. DisplayPlayerBlackedOutText::
  1155. ld hl, PlayerBlackedOutText
  1156. call PrintText
  1157. ld a, [wd732]
  1158. res 5, a ; reset forced to use bike bit
  1159. ld [wd732], a
  1160. jp HoldTextDisplayOpen
  1161. PlayerBlackedOutText::
  1162. TX_FAR _PlayerBlackedOutText
  1163. db "@"
  1164. DisplayRepelWoreOffText::
  1165. ld hl, RepelWoreOffText
  1166. call PrintText
  1167. jp AfterDisplayingTextID
  1168. RepelWoreOffText::
  1169. TX_FAR _RepelWoreOffText
  1170. db "@"
  1171. INCLUDE "engine/menu/start_menu.asm"
  1172. ; function to count how many bits are set in a string of bytes
  1173. ; INPUT:
  1174. ; hl = address of string of bytes
  1175. ; b = length of string of bytes
  1176. ; OUTPUT:
  1177. ; [wNumSetBits] = number of set bits
  1178. CountSetBits::
  1179. ld c, 0
  1180. .loop
  1181. ld a, [hli]
  1182. ld e, a
  1183. ld d, 8
  1184. .innerLoop ; count how many bits are set in the current byte
  1185. srl e
  1186. ld a, 0
  1187. adc c
  1188. ld c, a
  1189. dec d
  1190. jr nz, .innerLoop
  1191. dec b
  1192. jr nz, .loop
  1193. ld a, c
  1194. ld [wNumSetBits], a
  1195. ret
  1196. ; subtracts the amount the player paid from their money
  1197. ; sets carry flag if there is enough money and unsets carry flag if not
  1198. SubtractAmountPaidFromMoney::
  1199. jpba SubtractAmountPaidFromMoney_
  1200. ; adds the amount the player sold to their money
  1201. AddAmountSoldToMoney::
  1202. ld de, wPlayerMoney + 2
  1203. ld hl, $ffa1 ; total price of items
  1204. ld c, 3 ; length of money in bytes
  1205. predef AddBCDPredef ; add total price to money
  1206. ld a, MONEY_BOX
  1207. ld [wTextBoxID], a
  1208. call DisplayTextBoxID ; redraw money text box
  1209. ld a, SFX_PURCHASE
  1210. call PlaySoundWaitForCurrent
  1211. jp WaitForSoundToFinish
  1212. ; function to remove an item (in varying quantities) from the player's bag or PC box
  1213. ; INPUT:
  1214. ; HL = address of inventory (either wNumBagItems or wNumBoxItems)
  1215. ; [wWhichPokemon] = index (within the inventory) of the item to remove
  1216. ; [wItemQuantity] = quantity to remove
  1217. RemoveItemFromInventory::
  1218. ld a, [H_LOADEDROMBANK]
  1219. push af
  1220. ld a, BANK(RemoveItemFromInventory_)
  1221. ld [H_LOADEDROMBANK], a
  1222. ld [MBC1RomBank], a
  1223. call RemoveItemFromInventory_
  1224. pop af
  1225. ld [H_LOADEDROMBANK], a
  1226. ld [MBC1RomBank], a
  1227. ret
  1228. ; function to add an item (in varying quantities) to the player's bag or PC box
  1229. ; INPUT:
  1230. ; HL = address of inventory (either wNumBagItems or wNumBoxItems)
  1231. ; [wcf91] = item ID
  1232. ; [wItemQuantity] = item quantity
  1233. ; sets carry flag if successful, unsets carry flag if unsuccessful
  1234. AddItemToInventory::
  1235. push bc
  1236. ld a, [H_LOADEDROMBANK]
  1237. push af
  1238. ld a, BANK(AddItemToInventory_)
  1239. ld [H_LOADEDROMBANK], a
  1240. ld [MBC1RomBank], a
  1241. call AddItemToInventory_
  1242. pop bc
  1243. ld a, b
  1244. ld [H_LOADEDROMBANK], a
  1245. ld [MBC1RomBank], a
  1246. pop bc
  1247. ret
  1248. ; INPUT:
  1249. ; [wListMenuID] = list menu ID
  1250. ; [wListPointer] = address of the list (2 bytes)
  1251. DisplayListMenuID::
  1252. xor a
  1253. ld [H_AUTOBGTRANSFERENABLED], a ; disable auto-transfer
  1254. ld a, 1
  1255. ld [hJoy7], a ; joypad state update flag
  1256. ld a, [wBattleType]
  1257. and a ; is it the Old Man battle?
  1258. jr nz, .specialBattleType
  1259. ld a, $01 ; hardcoded bank
  1260. jr .bankswitch
  1261. .specialBattleType ; Old Man battle
  1262. ld a, BANK(DisplayBattleMenu)
  1263. .bankswitch
  1264. call BankswitchHome
  1265. ld hl, wd730
  1266. set 6, [hl] ; turn off letter printing delay
  1267. xor a
  1268. ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
  1269. ld [wListCount], a
  1270. ld a, [wListPointer]
  1271. ld l, a
  1272. ld a, [wListPointer + 1]
  1273. ld h, a ; hl = address of the list
  1274. ld a, [hl] ; the first byte is the number of entries in the list
  1275. ld [wListCount], a
  1276. ld a, LIST_MENU_BOX
  1277. ld [wTextBoxID], a
  1278. call DisplayTextBoxID ; draw the menu text box
  1279. call UpdateSprites ; disable sprites behind the text box
  1280. ; the code up to .skipMovingSprites appears to be useless
  1281. coord hl, 4, 2 ; coordinates of upper left corner of menu text box
  1282. lb de, 9, 14 ; height and width of menu text box
  1283. ld a, [wListMenuID]
  1284. and a ; is it a PC pokemon list?
  1285. jr nz, .skipMovingSprites
  1286. call UpdateSprites
  1287. .skipMovingSprites
  1288. ld a, 1 ; max menu item ID is 1 if the list has less than 2 entries
  1289. ld [wMenuWatchMovingOutOfBounds], a
  1290. ld a, [wListCount]
  1291. cp 2 ; does the list have less than 2 entries?
  1292. jr c, .setMenuVariables
  1293. ld a, 2 ; max menu item ID is 2 if the list has at least 2 entries
  1294. .setMenuVariables
  1295. ld [wMaxMenuItem], a
  1296. ld a, 4
  1297. ld [wTopMenuItemY], a
  1298. ld a, 5
  1299. ld [wTopMenuItemX], a
  1300. ld a, A_BUTTON | B_BUTTON | SELECT
  1301. ld [wMenuWatchedKeys], a
  1302. ld c, 10
  1303. call DelayFrames
  1304. DisplayListMenuIDLoop::
  1305. xor a
  1306. ld [H_AUTOBGTRANSFERENABLED], a ; disable transfer
  1307. call PrintListMenuEntries
  1308. ld a, 1
  1309. ld [H_AUTOBGTRANSFERENABLED], a ; enable transfer
  1310. call Delay3
  1311. ld a, [wBattleType]
  1312. and a ; is it the Old Man battle?
  1313. jr z, .notOldManBattle
  1314. .oldManBattle
  1315. ld a, "▶"
  1316. Coorda 5, 4 ; place menu cursor in front of first menu entry
  1317. ld c, 80
  1318. call DelayFrames
  1319. xor a
  1320. ld [wCurrentMenuItem], a
  1321. coord hl, 5, 4
  1322. ld a, l
  1323. ld [wMenuCursorLocation], a
  1324. ld a, h
  1325. ld [wMenuCursorLocation + 1], a
  1326. jr .buttonAPressed
  1327. .notOldManBattle
  1328. call LoadGBPal
  1329. call HandleMenuInput
  1330. push af
  1331. call PlaceMenuCursor
  1332. pop af
  1333. bit 0, a ; was the A button pressed?
  1334. jp z, .checkOtherKeys
  1335. .buttonAPressed
  1336. ld a, [wCurrentMenuItem]
  1337. call PlaceUnfilledArrowMenuCursor
  1338. ; pointless because both values are overwritten before they are read
  1339. ld a, $01
  1340. ld [wMenuExitMethod], a
  1341. ld [wChosenMenuItem], a
  1342. xor a
  1343. ld [wMenuWatchMovingOutOfBounds], a
  1344. ld a, [wCurrentMenuItem]
  1345. ld c, a
  1346. ld a, [wListScrollOffset]
  1347. add c
  1348. ld c, a
  1349. ld a, [wListCount]
  1350. and a ; is the list empty?
  1351. jp z, ExitListMenu ; if so, exit the menu
  1352. dec a
  1353. cp c ; did the player select Cancel?
  1354. jp c, ExitListMenu ; if so, exit the menu
  1355. ld a, c
  1356. ld [wWhichPokemon], a
  1357. ld a, [wListMenuID]
  1358. cp ITEMLISTMENU
  1359. jr nz, .skipMultiplying
  1360. ; if it's an item menu
  1361. sla c ; item entries are 2 bytes long, so multiply by 2
  1362. .skipMultiplying
  1363. ld a, [wListPointer]
  1364. ld l, a
  1365. ld a, [wListPointer + 1]
  1366. ld h, a
  1367. inc hl ; hl = beginning of list entries
  1368. ld b, 0
  1369. add hl, bc
  1370. ld a, [hl]
  1371. ld [wcf91], a
  1372. ld a, [wListMenuID]
  1373. and a ; is it a PC pokemon list?
  1374. jr z, .pokemonList
  1375. push hl
  1376. call GetItemPrice
  1377. pop hl
  1378. ld a, [wListMenuID]
  1379. cp ITEMLISTMENU
  1380. jr nz, .skipGettingQuantity
  1381. ; if it's an item menu
  1382. inc hl
  1383. ld a, [hl] ; a = item quantity
  1384. ld [wMaxItemQuantity], a
  1385. .skipGettingQuantity
  1386. ld a, [wcf91]
  1387. ld [wd0b5], a
  1388. ld a, BANK(ItemNames)
  1389. ld [wPredefBank], a
  1390. call GetName
  1391. jr .storeChosenEntry
  1392. .pokemonList
  1393. ld hl, wPartyCount
  1394. ld a, [wListPointer]
  1395. cp l ; is it a list of party pokemon or box pokemon?
  1396. ld hl, wPartyMonNicks
  1397. jr z, .getPokemonName
  1398. ld hl, wBoxMonNicks ; box pokemon names
  1399. .getPokemonName
  1400. ld a, [wWhichPokemon]
  1401. call GetPartyMonName
  1402. .storeChosenEntry ; store the menu entry that the player chose and return
  1403. ld de, wcd6d
  1404. call CopyStringToCF4B ; copy name to wcf4b
  1405. ld a, CHOSE_MENU_ITEM
  1406. ld [wMenuExitMethod], a
  1407. ld a, [wCurrentMenuItem]
  1408. ld [wChosenMenuItem], a
  1409. xor a
  1410. ld [hJoy7], a ; joypad state update flag
  1411. ld hl, wd730
  1412. res 6, [hl] ; turn on letter printing delay
  1413. jp BankswitchBack
  1414. .checkOtherKeys ; check B, SELECT, Up, and Down keys
  1415. bit 1, a ; was the B button pressed?
  1416. jp nz, ExitListMenu ; if so, exit the menu
  1417. bit 2, a ; was the select button pressed?
  1418. jp nz, HandleItemListSwapping ; if so, allow the player to swap menu entries
  1419. ld b, a
  1420. bit 7, b ; was Down pressed?
  1421. ld hl, wListScrollOffset
  1422. jr z, .upPressed
  1423. .downPressed
  1424. ld a, [hl]
  1425. add 3
  1426. ld b, a
  1427. ld a, [wListCount]
  1428. cp b ; will going down scroll past the Cancel button?
  1429. jp c, DisplayListMenuIDLoop
  1430. inc [hl] ; if not, go down
  1431. jp DisplayListMenuIDLoop
  1432. .upPressed
  1433. ld a, [hl]
  1434. and a
  1435. jp z, DisplayListMenuIDLoop
  1436. dec [hl]
  1437. jp DisplayListMenuIDLoop
  1438. DisplayChooseQuantityMenu::
  1439. ; text box dimensions/coordinates for just quantity
  1440. coord hl, 15, 9
  1441. ld b, 1 ; height
  1442. ld c, 3 ; width
  1443. ld a, [wListMenuID]
  1444. cp PRICEDITEMLISTMENU
  1445. jr nz, .drawTextBox
  1446. ; text box dimensions/coordinates for quantity and price
  1447. coord hl, 7, 9
  1448. ld b, 1 ; height
  1449. ld c, 11 ; width
  1450. .drawTextBox
  1451. call TextBoxBorder
  1452. coord hl, 16, 10
  1453. ld a, [wListMenuID]
  1454. cp PRICEDITEMLISTMENU
  1455. jr nz, .printInitialQuantity
  1456. coord hl, 8, 10
  1457. .printInitialQuantity
  1458. ld de, InitialQuantityText
  1459. call PlaceString
  1460. xor a
  1461. ld [wItemQuantity], a ; initialize current quantity to 0
  1462. jp .incrementQuantity
  1463. .waitForKeyPressLoop
  1464. call JoypadLowSensitivity
  1465. ld a, [hJoyPressed] ; newly pressed buttons
  1466. bit 0, a ; was the A button pressed?
  1467. jp nz, .buttonAPressed
  1468. bit 1, a ; was the B button pressed?
  1469. jp nz, .buttonBPressed
  1470. bit 6, a ; was Up pressed?
  1471. jr nz, .incrementQuantity
  1472. bit 7, a ; was Down pressed?
  1473. jr nz, .decrementQuantity
  1474. jr .waitForKeyPressLoop
  1475. .incrementQuantity
  1476. ld a, [wMaxItemQuantity]
  1477. inc a
  1478. ld b, a
  1479. ld hl, wItemQuantity ; current quantity
  1480. inc [hl]
  1481. ld a, [hl]
  1482. cp b
  1483. jr nz, .handleNewQuantity
  1484. ; wrap to 1 if the player goes above the max quantity
  1485. ld a, 1
  1486. ld [hl], a
  1487. jr .handleNewQuantity
  1488. .decrementQuantity
  1489. ld hl, wItemQuantity ; current quantity
  1490. dec [hl]
  1491. jr nz, .handleNewQuantity
  1492. ; wrap to the max quantity if the player goes below 1
  1493. ld a, [wMaxItemQuantity]
  1494. ld [hl], a
  1495. .handleNewQuantity
  1496. coord hl, 17, 10
  1497. ld a, [wListMenuID]
  1498. cp PRICEDITEMLISTMENU
  1499. jr nz, .printQuantity
  1500. .printPrice
  1501. ld c, $03
  1502. ld a, [wItemQuantity]
  1503. ld b, a
  1504. ld hl, hMoney ; total price
  1505. ; initialize total price to 0
  1506. xor a
  1507. ld [hli], a
  1508. ld [hli], a
  1509. ld [hl], a
  1510. .addLoop ; loop to multiply the individual price by the quantity to get the total price
  1511. ld de, hMoney + 2
  1512. ld hl, hItemPrice + 2
  1513. push bc
  1514. predef AddBCDPredef ; add the individual price to the current sum
  1515. pop bc
  1516. dec b
  1517. jr nz, .addLoop
  1518. ld a, [hHalveItemPrices]
  1519. and a ; should the price be halved (for selling items)?
  1520. jr z, .skipHalvingPrice
  1521. xor a
  1522. ld [hDivideBCDDivisor], a
  1523. ld [hDivideBCDDivisor + 1], a
  1524. ld a, $02
  1525. ld [hDivideBCDDivisor + 2], a
  1526. predef DivideBCDPredef3 ; halves the price
  1527. ; store the halved price
  1528. ld a, [hDivideBCDQuotient]
  1529. ld [hMoney], a
  1530. ld a, [hDivideBCDQuotient + 1]
  1531. ld [hMoney + 1], a
  1532. ld a, [hDivideBCDQuotient + 2]
  1533. ld [hMoney + 2], a
  1534. .skipHalvingPrice
  1535. coord hl, 12, 10
  1536. ld de, SpacesBetweenQuantityAndPriceText
  1537. call PlaceString
  1538. ld de, hMoney ; total price
  1539. ld c, $a3
  1540. call PrintBCDNumber
  1541. coord hl, 9, 10
  1542. .printQuantity
  1543. ld de, wItemQuantity ; current quantity
  1544. lb bc, LEADING_ZEROES | 1, 2 ; 1 byte, 2 digits
  1545. call PrintNumber
  1546. jp .waitForKeyPressLoop
  1547. .buttonAPressed ; the player chose to make the transaction
  1548. xor a
  1549. ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
  1550. ret
  1551. .buttonBPressed ; the player chose to cancel the transaction
  1552. xor a
  1553. ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
  1554. ld a, $ff
  1555. ret
  1556. InitialQuantityText::
  1557. db "×01@"
  1558. SpacesBetweenQuantityAndPriceText::
  1559. db " @"
  1560. ExitListMenu::
  1561. ld a, [wCurrentMenuItem]
  1562. ld [wChosenMenuItem], a
  1563. ld a, CANCELLED_MENU
  1564. ld [wMenuExitMethod], a
  1565. ld [wMenuWatchMovingOutOfBounds], a
  1566. xor a
  1567. ld [hJoy7], a
  1568. ld hl, wd730
  1569. res 6, [hl]
  1570. call BankswitchBack
  1571. xor a
  1572. ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
  1573. scf
  1574. ret
  1575. PrintListMenuEntries::
  1576. coord hl, 5, 3
  1577. ld b, 9
  1578. ld c, 14
  1579. call ClearScreenArea
  1580. ld a, [wListPointer]
  1581. ld e, a
  1582. ld a, [wListPointer + 1]
  1583. ld d, a
  1584. inc de ; de = beginning of list entries
  1585. ld a, [wListScrollOffset]
  1586. ld c, a
  1587. ld a, [wListMenuID]
  1588. cp ITEMLISTMENU
  1589. ld a, c
  1590. jr nz, .skipMultiplying
  1591. ; if it's an item menu
  1592. ; item entries are 2 bytes long, so multiply by 2
  1593. sla a
  1594. sla c
  1595. .skipMultiplying
  1596. add e
  1597. ld e, a
  1598. jr nc, .noCarry
  1599. inc d
  1600. .noCarry
  1601. coord hl, 6, 4 ; coordinates of first list entry name
  1602. ld b, 4 ; print 4 names
  1603. .loop
  1604. ld a, b
  1605. ld [wWhichPokemon], a
  1606. ld a, [de]
  1607. ld [wd11e], a
  1608. cp $ff
  1609. jp z, .printCancelMenuItem
  1610. push bc
  1611. push de
  1612. push hl
  1613. push hl
  1614. push de
  1615. ld a, [wListMenuID]
  1616. and a
  1617. jr z, .pokemonPCMenu
  1618. cp MOVESLISTMENU
  1619. jr z, .movesMenu
  1620. .itemMenu
  1621. call GetItemName
  1622. jr .placeNameString
  1623. .pokemonPCMenu
  1624. push hl
  1625. ld hl, wPartyCount
  1626. ld a, [wListPointer]
  1627. cp l ; is it a list of party pokemon or box pokemon?
  1628. ld hl, wPartyMonNicks
  1629. jr z, .getPokemonName
  1630. ld hl, wBoxMonNicks ; box pokemon names
  1631. .getPokemonName
  1632. ld a, [wWhichPokemon]
  1633. ld b, a
  1634. ld a, 4
  1635. sub b
  1636. ld b, a
  1637. ld a, [wListScrollOffset]
  1638. add b
  1639. call GetPartyMonName
  1640. pop hl
  1641. jr .placeNameString
  1642. .movesMenu
  1643. call GetMoveName
  1644. .placeNameString
  1645. call PlaceString
  1646. pop de
  1647. pop hl
  1648. ld a, [wPrintItemPrices]
  1649. and a ; should prices be printed?
  1650. jr z, .skipPrintingItemPrice
  1651. .printItemPrice
  1652. push hl
  1653. ld a, [de]
  1654. ld de, ItemPrices
  1655. ld [wcf91], a
  1656. call GetItemPrice ; get price
  1657. pop hl
  1658. ld bc, SCREEN_WIDTH + 5 ; 1 row down and 5 columns right
  1659. add hl, bc
  1660. ld c, $a3 ; no leading zeroes, right-aligned, print currency symbol, 3 bytes
  1661. call PrintBCDNumber
  1662. .skipPrintingItemPrice
  1663. ld a, [wListMenuID]
  1664. and a
  1665. jr nz, .skipPrintingPokemonLevel
  1666. .printPokemonLevel
  1667. ld a, [wd11e]
  1668. push af
  1669. push hl
  1670. ld hl, wPartyCount
  1671. ld a, [wListPointer]
  1672. cp l ; is it a list of party pokemon or box pokemon?
  1673. ld a, PLAYER_PARTY_DATA
  1674. jr z, .next
  1675. ld a, BOX_DATA
  1676. .next
  1677. ld [wMonDataLocation], a
  1678. ld hl, wWhichPokemon
  1679. ld a, [hl]
  1680. ld b, a
  1681. ld a, $04
  1682. sub b
  1683. ld b, a
  1684. ld a, [wListScrollOffset]
  1685. add b
  1686. ld [hl], a
  1687. call LoadMonData
  1688. ld a, [wMonDataLocation]
  1689. and a ; is it a list of party pokemon or box pokemon?
  1690. jr z, .skipCopyingLevel
  1691. .copyLevel
  1692. ld a, [wLoadedMonBoxLevel]
  1693. ld [wLoadedMonLevel], a
  1694. .skipCopyingLevel
  1695. pop hl
  1696. ld bc, $001c
  1697. add hl, bc
  1698. call PrintLevel
  1699. pop af
  1700. ld [wd11e], a
  1701. .skipPrintingPokemonLevel
  1702. pop hl
  1703. pop de
  1704. inc de
  1705. ld a, [wListMenuID]
  1706. cp ITEMLISTMENU
  1707. jr nz, .nextListEntry
  1708. .printItemQuantity
  1709. ld a, [wd11e]
  1710. ld [wcf91], a
  1711. call IsKeyItem ; check if item is unsellable
  1712. ld a, [wIsKeyItem]
  1713. and a ; is the item unsellable?
  1714. jr nz, .skipPrintingItemQuantity ; if so, don't print the quantity
  1715. push hl
  1716. ld bc, SCREEN_WIDTH + 8 ; 1 row down and 8 columns right
  1717. add hl, bc
  1718. ld a, "×"
  1719. ld [hli], a
  1720. ld a, [wd11e]
  1721. push af
  1722. ld a, [de]
  1723. ld [wMaxItemQuantity], a
  1724. push de
  1725. ld de, wd11e
  1726. ld [de], a
  1727. lb bc, 1, 2
  1728. call PrintNumber
  1729. pop de
  1730. pop af
  1731. ld [wd11e], a
  1732. pop hl
  1733. .skipPrintingItemQuantity
  1734. inc de
  1735. pop bc
  1736. inc c
  1737. push bc
  1738. inc c
  1739. ld a, [wMenuItemToSwap] ; ID of item chosen for swapping (counts from 1)
  1740. and a ; is an item being swapped?
  1741. jr z, .nextListEntry
  1742. sla a
  1743. cp c ; is it this item?
  1744. jr nz, .nextListEntry
  1745. dec hl
  1746. ld a, $ec ; unfilled right arrow menu cursor to indicate an item being swapped
  1747. ld [hli], a
  1748. .nextListEntry
  1749. ld bc, 2 * SCREEN_WIDTH ; 2 rows
  1750. add hl, bc
  1751. pop bc
  1752. inc c
  1753. dec b
  1754. jp nz, .loop
  1755. ld bc, -8
  1756. add hl, bc
  1757. ld a, "▼"
  1758. ld [hl], a
  1759. ret
  1760. .printCancelMenuItem
  1761. ld de, ListMenuCancelText
  1762. jp PlaceString
  1763. ListMenuCancelText::
  1764. db "CANCEL@"
  1765. GetMonName::
  1766. push hl
  1767. ld a, [H_LOADEDROMBANK]
  1768. push af
  1769. ld a, BANK(MonsterNames)
  1770. ld [H_LOADEDROMBANK], a
  1771. ld [MBC1RomBank], a
  1772. ld a, [wd11e]
  1773. dec a
  1774. ld hl, MonsterNames
  1775. ld c, 10
  1776. ld b, 0
  1777. call AddNTimes
  1778. ld de, wcd6d
  1779. push de
  1780. ld bc, 10
  1781. call CopyData
  1782. ld hl, wcd6d + 10
  1783. ld [hl], "@"
  1784. pop de
  1785. pop af
  1786. ld [H_LOADEDROMBANK], a
  1787. ld [MBC1RomBank], a
  1788. pop hl
  1789. ret
  1790. GetItemName::
  1791. ; given an item ID at [wd11e], store the name of the item into a string
  1792. ; starting at wcd6d
  1793. push hl
  1794. push bc
  1795. ld a, [wd11e]
  1796. cp HM_01 ; is this a TM/HM?
  1797. jr nc, .Machine
  1798. ld [wd0b5], a
  1799. ld a, ITEM_NAME
  1800. ld [wNameListType], a
  1801. ld a, BANK(ItemNames)
  1802. ld [wPredefBank], a
  1803. call GetName
  1804. jr .Finish
  1805. .Machine
  1806. call GetMachineName
  1807. .Finish
  1808. ld de, wcd6d ; pointer to where item name is stored in RAM
  1809. pop bc
  1810. pop hl
  1811. ret
  1812. GetMachineName::
  1813. ; copies the name of the TM/HM in [wd11e] to wcd6d
  1814. push hl
  1815. push de
  1816. push bc
  1817. ld a, [wd11e]
  1818. push af
  1819. cp TM_01 ; is this a TM? [not HM]
  1820. jr nc, .WriteTM
  1821. ; if HM, then write "HM" and add 5 to the item ID, so we can reuse the
  1822. ; TM printing code
  1823. add 5
  1824. ld [wd11e], a
  1825. ld hl, HiddenPrefix ; points to "HM"
  1826. ld bc, 2
  1827. jr .WriteMachinePrefix
  1828. .WriteTM
  1829. ld hl, TechnicalPrefix ; points to "TM"
  1830. ld bc, 2
  1831. .WriteMachinePrefix
  1832. ld de, wcd6d
  1833. call CopyData
  1834. ; now get the machine number and convert it to text
  1835. ld a, [wd11e]
  1836. sub TM_01 - 1
  1837. ld b, "0"
  1838. .FirstDigit
  1839. sub 10
  1840. jr c, .SecondDigit
  1841. inc b
  1842. jr .FirstDigit
  1843. .SecondDigit
  1844. add 10
  1845. push af
  1846. ld a, b
  1847. ld [de], a
  1848. inc de
  1849. pop af
  1850. ld b, "0"
  1851. add b
  1852. ld [de], a
  1853. inc de
  1854. ld a, "@"
  1855. ld [de], a
  1856. pop af
  1857. ld [wd11e], a
  1858. pop bc
  1859. pop de
  1860. pop hl
  1861. ret
  1862. TechnicalPrefix::
  1863. db "TM"
  1864. HiddenPrefix::
  1865. db "HM"
  1866. ; sets carry if item is HM, clears carry if item is not HM
  1867. ; Input: a = item ID
  1868. IsItemHM::
  1869. cp HM_01
  1870. jr c, .notHM
  1871. cp TM_01
  1872. ret
  1873. .notHM
  1874. and a
  1875. ret
  1876. ; sets carry if move is an HM, clears carry if move is not an HM
  1877. ; Input: a = move ID
  1878. IsMoveHM::
  1879. ld hl, HMMoves
  1880. ld de, 1
  1881. jp IsInArray
  1882. HMMoves::
  1883. db CUT,FLY,SURF,STRENGTH,FLASH
  1884. db $ff ; terminator
  1885. GetMoveName::
  1886. push hl
  1887. ld a, MOVE_NAME
  1888. ld [wNameListType], a
  1889. ld a, [wd11e]
  1890. ld [wd0b5], a
  1891. ld a, BANK(MoveNames)
  1892. ld [wPredefBank], a
  1893. call GetName
  1894. ld de, wcd6d ; pointer to where move name is stored in RAM
  1895. pop hl
  1896. ret
  1897. ; reloads text box tile patterns, current map view, and tileset tile patterns
  1898. ReloadMapData::
  1899. ld a, [H_LOADEDROMBANK]
  1900. push af
  1901. ld a, [wCurMap]
  1902. call SwitchToMapRomBank
  1903. call DisableLCD
  1904. call LoadTextBoxTilePatterns
  1905. call LoadCurrentMapView
  1906. call LoadTilesetTilePatternData
  1907. call EnableLCD
  1908. pop af
  1909. ld [H_LOADEDROMBANK], a
  1910. ld [MBC1RomBank], a
  1911. ret
  1912. ; reloads tileset tile patterns
  1913. ReloadTilesetTilePatterns::
  1914. ld a, [H_LOADEDROMBANK]
  1915. push af
  1916. ld a, [wCurMap]
  1917. call SwitchToMapRomBank
  1918. call DisableLCD
  1919. call LoadTilesetTilePatternData
  1920. call EnableLCD
  1921. pop af
  1922. ld [H_LOADEDROMBANK], a
  1923. ld [MBC1RomBank], a
  1924. ret
  1925. ; shows the town map and lets the player choose a destination to fly to
  1926. ChooseFlyDestination::
  1927. ld hl, wd72e
  1928. res 4, [hl]
  1929. jpba LoadTownMap_Fly
  1930. ; causes the text box to close without waiting for a button press after displaying text
  1931. DisableWaitingAfterTextDisplay::
  1932. ld a, $01
  1933. ld [wDoNotWaitForButtonPressAfterDisplayingText], a
  1934. ret
  1935. ; uses an item
  1936. ; UseItem is used with dummy items to perform certain other functions as well
  1937. ; INPUT:
  1938. ; [wcf91] = item ID
  1939. ; OUTPUT:
  1940. ; [wActionResultOrTookBattleTurn] = success
  1941. ; 00: unsuccessful
  1942. ; 01: successful
  1943. ; 02: not able to be used right now, no extra menu displayed (only certain items use this)
  1944. UseItem::
  1945. jpba UseItem_
  1946. ; confirms the item toss and then tosses the item
  1947. ; INPUT:
  1948. ; hl = address of inventory (either wNumBagItems or wNumBoxItems)
  1949. ; [wcf91] = item ID
  1950. ; [wWhichPokemon] = index of item within inventory
  1951. ; [wItemQuantity] = quantity to toss
  1952. ; OUTPUT:
  1953. ; clears carry flag if the item is tossed, sets carry flag if not
  1954. TossItem::
  1955. ld a, [H_LOADEDROMBANK]
  1956. push af
  1957. ld a, BANK(TossItem_)
  1958. ld [H_LOADEDROMBANK], a
  1959. ld [MBC1RomBank], a
  1960. call TossItem_
  1961. pop de
  1962. ld a, d
  1963. ld [H_LOADEDROMBANK], a
  1964. ld [MBC1RomBank], a
  1965. ret
  1966. ; checks if an item is a key item
  1967. ; INPUT:
  1968. ; [wcf91] = item ID
  1969. ; OUTPUT:
  1970. ; [wIsKeyItem] = result
  1971. ; 00: item is not key item
  1972. ; 01: item is key item
  1973. IsKeyItem::
  1974. push hl
  1975. push de
  1976. push bc
  1977. callba IsKeyItem_
  1978. pop bc
  1979. pop de
  1980. pop hl
  1981. ret
  1982. ; function to draw various text boxes
  1983. ; INPUT:
  1984. ; [wTextBoxID] = text box ID
  1985. ; b, c = y, x cursor position (TWO_OPTION_MENU only)
  1986. DisplayTextBoxID::
  1987. ld a, [H_LOADEDROMBANK]
  1988. push af
  1989. ld a, BANK(DisplayTextBoxID_)
  1990. ld [H_LOADEDROMBANK], a
  1991. ld [MBC1RomBank], a
  1992. call DisplayTextBoxID_
  1993. pop bc
  1994. ld a, b
  1995. ld [H_LOADEDROMBANK], a
  1996. ld [MBC1RomBank], a
  1997. ret
  1998. ; not zero if an NPC movement script is running, the player character is
  1999. ; automatically stepping down from a door, or joypad states are being simulated
  2000. IsPlayerCharacterBeingControlledByGame::
  2001. ld a, [wNPCMovementScriptPointerTableNum]
  2002. and a
  2003. ret nz
  2004. ld a, [wd736]
  2005. bit 1, a ; currently stepping down from door bit
  2006. ret nz
  2007. ld a, [wd730]
  2008. and $80
  2009. ret
  2010. RunNPCMovementScript::
  2011. ld hl, wd736
  2012. bit 0, [hl]
  2013. res 0, [hl]
  2014. jr nz, .playerStepOutFromDoor
  2015. ld a, [wNPCMovementScriptPointerTableNum]
  2016. and a
  2017. ret z
  2018. dec a
  2019. add a
  2020. ld d, 0
  2021. ld e, a
  2022. ld hl, .NPCMovementScriptPointerTables
  2023. add hl, de
  2024. ld a, [hli]
  2025. ld h, [hl]
  2026. ld l, a
  2027. ld a, [H_LOADEDROMBANK]
  2028. push af
  2029. ld a, [wNPCMovementScriptBank]
  2030. ld [H_LOADEDROMBANK], a
  2031. ld [MBC1RomBank], a
  2032. ld a, [wNPCMovementScriptFunctionNum]
  2033. call CallFunctionInTable
  2034. pop af
  2035. ld [H_LOADEDROMBANK], a
  2036. ld [MBC1RomBank], a
  2037. ret
  2038. .NPCMovementScriptPointerTables
  2039. dw PalletMovementScriptPointerTable
  2040. dw PewterMuseumGuyMovementScriptPointerTable
  2041. dw PewterGymGuyMovementScriptPointerTable
  2042. .playerStepOutFromDoor
  2043. jpba PlayerStepOutFromDoor
  2044. EndNPCMovementScript::
  2045. jpba _EndNPCMovementScript
  2046. EmptyFunc2::
  2047. ret
  2048. ; stores hl in [wTrainerHeaderPtr]
  2049. StoreTrainerHeaderPointer::
  2050. ld a, h
  2051. ld [wTrainerHeaderPtr], a
  2052. ld a, l
  2053. ld [wTrainerHeaderPtr+1], a
  2054. ret
  2055. ; executes the current map script from the function pointer array provided in hl.
  2056. ; a: map script index to execute (unless overridden by [wd733] bit 4)
  2057. ExecuteCurMapScriptInTable::
  2058. push af
  2059. push de
  2060. call StoreTrainerHeaderPointer
  2061. pop hl
  2062. pop af
  2063. push hl
  2064. ld hl, wFlags_D733
  2065. bit 4, [hl]
  2066. res 4, [hl]
  2067. jr z, .useProvidedIndex ; test if map script index was overridden manually
  2068. ld a, [wCurMapScript]
  2069. .useProvidedIndex
  2070. pop hl
  2071. ld [wCurMapScript], a
  2072. call CallFunctionInTable
  2073. ld a, [wCurMapScript]
  2074. ret
  2075. LoadGymLeaderAndCityName::
  2076. push de
  2077. ld de, wGymCityName
  2078. ld bc, $11
  2079. call CopyData ; load city name
  2080. pop hl
  2081. ld de, wGymLeaderName
  2082. ld bc, NAME_LENGTH
  2083. jp CopyData ; load gym leader name
  2084. ; reads specific information from trainer header (pointed to at wTrainerHeaderPtr)
  2085. ; a: offset in header data
  2086. ; 0 -> flag's bit (into wTrainerHeaderFlagBit)
  2087. ; 2 -> flag's byte ptr (into hl)
  2088. ; 4 -> before battle text (into hl)
  2089. ; 6 -> after battle text (into hl)
  2090. ; 8 -> end battle text (into hl)
  2091. ReadTrainerHeaderInfo::
  2092. push de
  2093. push af
  2094. ld d, $0
  2095. ld e, a
  2096. ld hl, wTrainerHeaderPtr
  2097. ld a, [hli]
  2098. ld l, [hl]
  2099. ld h, a
  2100. add hl, de
  2101. pop af
  2102. and a
  2103. jr nz, .nonZeroOffset
  2104. ld a, [hl]
  2105. ld [wTrainerHeaderFlagBit], a ; store flag's bit
  2106. jr .done
  2107. .nonZeroOffset
  2108. cp $2
  2109. jr z, .readPointer ; read flag's byte ptr
  2110. cp $4
  2111. jr z, .readPointer ; read before battle text
  2112. cp $6
  2113. jr z, .readPointer ; read after battle text
  2114. cp $8
  2115. jr z, .readPointer ; read end battle text
  2116. cp $a
  2117. jr nz, .done
  2118. ld a, [hli] ; read end battle text (2) but override the result afterwards (XXX why, bug?)
  2119. ld d, [hl]
  2120. ld e, a
  2121. jr .done
  2122. .readPointer
  2123. ld a, [hli]
  2124. ld h, [hl]
  2125. ld l, a
  2126. .done
  2127. pop de
  2128. ret
  2129. TrainerFlagAction::
  2130. predef_jump FlagActionPredef
  2131. TalkToTrainer::
  2132. call StoreTrainerHeaderPointer
  2133. xor a
  2134. call ReadTrainerHeaderInfo ; read flag's bit
  2135. ld a, $2
  2136. call ReadTrainerHeaderInfo ; read flag's byte ptr
  2137. ld a, [wTrainerHeaderFlagBit]
  2138. ld c, a
  2139. ld b, FLAG_TEST
  2140. call TrainerFlagAction ; read trainer's flag
  2141. ld a, c
  2142. and a
  2143. jr z, .trainerNotYetFought ; test trainer's flag
  2144. ld a, $6
  2145. call ReadTrainerHeaderInfo ; print after battle text
  2146. jp PrintText
  2147. .trainerNotYetFought
  2148. ld a, $4
  2149. call ReadTrainerHeaderInfo ; print before battle text
  2150. call PrintText
  2151. ld a, $a
  2152. call ReadTrainerHeaderInfo ; (?) does nothing apparently (maybe bug in ReadTrainerHeaderInfo)
  2153. push de
  2154. ld a, $8
  2155. call ReadTrainerHeaderInfo ; read end battle text
  2156. pop de
  2157. call SaveEndBattleTextPointers
  2158. ld hl, wFlags_D733
  2159. set 4, [hl] ; activate map script index override (index is set below)
  2160. ld hl, wFlags_0xcd60
  2161. bit 0, [hl] ; test if player is already engaging the trainer (because the trainer saw the player)
  2162. ret nz
  2163. ; if the player talked to the trainer of his own volition
  2164. call EngageMapTrainer
  2165. ld hl, wCurMapScript
  2166. inc [hl] ; increment map script index before StartTrainerBattle increments it again (next script function is usually EndTrainerBattle)
  2167. jp StartTrainerBattle
  2168. ; checks if any trainers are seeing the player and wanting to fight
  2169. CheckFightingMapTrainers::
  2170. call CheckForEngagingTrainers
  2171. ld a, [wSpriteIndex]
  2172. cp $ff
  2173. jr nz, .trainerEngaging
  2174. xor a
  2175. ld [wSpriteIndex], a
  2176. ld [wTrainerHeaderFlagBit], a
  2177. ret
  2178. .trainerEngaging
  2179. ld hl, wFlags_D733
  2180. set 3, [hl]
  2181. ld [wEmotionBubbleSpriteIndex], a
  2182. xor a ; EXCLAMATION_BUBBLE
  2183. ld [wWhichEmotionBubble], a
  2184. predef EmotionBubble
  2185. ld a, D_RIGHT | D_LEFT | D_UP | D_DOWN
  2186. ld [wJoyIgnore], a
  2187. xor a
  2188. ld [hJoyHeld], a
  2189. call TrainerWalkUpToPlayer_Bank0
  2190. ld hl, wCurMapScript
  2191. inc [hl] ; increment map script index (next script function is usually DisplayEnemyTrainerTextAndStartBattle)
  2192. ret
  2193. ; display the before battle text after the enemy trainer has walked up to the player's sprite
  2194. DisplayEnemyTrainerTextAndStartBattle::
  2195. ld a, [wd730]
  2196. and $1
  2197. ret nz ; return if the enemy trainer hasn't finished walking to the player's sprite
  2198. ld [wJoyIgnore], a
  2199. ld a, [wSpriteIndex]
  2200. ld [hSpriteIndexOrTextID], a
  2201. call DisplayTextID
  2202. ; fall through
  2203. StartTrainerBattle::
  2204. xor a
  2205. ld [wJoyIgnore], a
  2206. call InitBattleEnemyParameters
  2207. ld hl, wd72d
  2208. set 6, [hl]
  2209. set 7, [hl]
  2210. ld hl, wd72e
  2211. set 1, [hl]
  2212. ld hl, wCurMapScript
  2213. inc [hl] ; increment map script index (next script function is usually EndTrainerBattle)
  2214. ret
  2215. EndTrainerBattle::
  2216. ld hl, wCurrentMapScriptFlags
  2217. set 5, [hl]
  2218. set 6, [hl]
  2219. ld hl, wd72d
  2220. res 7, [hl]
  2221. ld hl, wFlags_0xcd60
  2222. res 0, [hl] ; player is no longer engaged by any trainer
  2223. ld a, [wIsInBattle]
  2224. cp $ff
  2225. jp z, ResetButtonPressedAndMapScript
  2226. ld a, $2
  2227. call ReadTrainerHeaderInfo
  2228. ld a, [wTrainerHeaderFlagBit]
  2229. ld c, a
  2230. ld b, FLAG_SET
  2231. call TrainerFlagAction ; flag trainer as fought
  2232. ld a, [wEnemyMonOrTrainerClass]
  2233. cp 200
  2234. jr nc, .skipRemoveSprite ; test if trainer was fought (in that case skip removing the corresponding sprite)
  2235. ld hl, wMissableObjectList
  2236. ld de, $2
  2237. ld a, [wSpriteIndex]
  2238. call IsInArray ; search for sprite ID
  2239. inc hl
  2240. ld a, [hl]
  2241. ld [wMissableObjectIndex], a ; load corresponding missable object index and remove it
  2242. predef HideObject
  2243. .skipRemoveSprite
  2244. ld hl, wd730
  2245. bit 4, [hl]
  2246. res 4, [hl]
  2247. ret nz
  2248. ResetButtonPressedAndMapScript::
  2249. xor a
  2250. ld [wJoyIgnore], a
  2251. ld [hJoyHeld], a
  2252. ld [hJoyPressed], a
  2253. ld [hJoyReleased], a
  2254. ld [wCurMapScript], a ; reset battle status
  2255. ret
  2256. ; calls TrainerWalkUpToPlayer
  2257. TrainerWalkUpToPlayer_Bank0::
  2258. jpba TrainerWalkUpToPlayer
  2259. ; sets opponent type and mon set/lvl based on the engaging trainer data
  2260. InitBattleEnemyParameters::
  2261. ld a, [wEngagedTrainerClass]
  2262. ld [wCurOpponent], a
  2263. ld [wEnemyMonOrTrainerClass], a
  2264. cp 200
  2265. ld a, [wEngagedTrainerSet]
  2266. jr c, .noTrainer
  2267. ld [wTrainerNo], a
  2268. ret
  2269. .noTrainer
  2270. ld [wCurEnemyLVL], a
  2271. ret
  2272. GetSpritePosition1::
  2273. ld hl, _GetSpritePosition1
  2274. jr SpritePositionBankswitch
  2275. GetSpritePosition2::
  2276. ld hl, _GetSpritePosition2
  2277. jr SpritePositionBankswitch
  2278. SetSpritePosition1::
  2279. ld hl, _SetSpritePosition1
  2280. jr SpritePositionBankswitch
  2281. SetSpritePosition2::
  2282. ld hl, _SetSpritePosition2
  2283. SpritePositionBankswitch::
  2284. ld b, BANK(_GetSpritePosition1) ; BANK(_GetSpritePosition2), BANK(_SetSpritePosition1), BANK(_SetSpritePosition2)
  2285. jp Bankswitch ; indirect jump to one of the four functions
  2286. CheckForEngagingTrainers::
  2287. xor a
  2288. call ReadTrainerHeaderInfo ; read trainer flag's bit (unused)
  2289. ld d, h ; store trainer header address in de
  2290. ld e, l
  2291. .trainerLoop
  2292. call StoreTrainerHeaderPointer ; set trainer header pointer to current trainer
  2293. ld a, [de]
  2294. ld [wSpriteIndex], a ; store trainer flag's bit
  2295. ld [wTrainerHeaderFlagBit], a
  2296. cp $ff
  2297. ret z
  2298. ld a, $2
  2299. call ReadTrainerHeaderInfo ; read trainer flag's byte ptr
  2300. ld b, FLAG_TEST
  2301. ld a, [wTrainerHeaderFlagBit]
  2302. ld c, a
  2303. call TrainerFlagAction ; read trainer flag
  2304. ld a, c
  2305. and a ; has the trainer already been defeated?
  2306. jr nz, .continue
  2307. push hl
  2308. push de
  2309. push hl
  2310. xor a
  2311. call ReadTrainerHeaderInfo ; get trainer header pointer
  2312. inc hl
  2313. ld a, [hl] ; read trainer engage distance
  2314. pop hl
  2315. ld [wTrainerEngageDistance], a
  2316. ld a, [wSpriteIndex]
  2317. swap a
  2318. ld [wTrainerSpriteOffset], a
  2319. predef TrainerEngage
  2320. pop de
  2321. pop hl
  2322. ld a, [wTrainerSpriteOffset]
  2323. and a
  2324. ret nz ; break if the trainer is engaging
  2325. .continue
  2326. ld hl, $c
  2327. add hl, de
  2328. ld d, h
  2329. ld e, l
  2330. jr .trainerLoop
  2331. ; hl = text if the player wins
  2332. ; de = text if the player loses
  2333. SaveEndBattleTextPointers::
  2334. ld a, [H_LOADEDROMBANK]
  2335. ld [wEndBattleTextRomBank], a
  2336. ld a, h
  2337. ld [wEndBattleWinTextPointer], a
  2338. ld a, l
  2339. ld [wEndBattleWinTextPointer + 1], a
  2340. ld a, d
  2341. ld [wEndBattleLoseTextPointer], a
  2342. ld a, e
  2343. ld [wEndBattleLoseTextPointer + 1], a
  2344. ret
  2345. ; loads data of some trainer on the current map and plays pre-battle music
  2346. ; [wSpriteIndex]: sprite ID of trainer who is engaged
  2347. EngageMapTrainer::
  2348. ld hl, wMapSpriteExtraData
  2349. ld d, $0
  2350. ld a, [wSpriteIndex]
  2351. dec a
  2352. add a
  2353. ld e, a
  2354. add hl, de ; seek to engaged trainer data
  2355. ld a, [hli] ; load trainer class
  2356. ld [wEngagedTrainerClass], a
  2357. ld a, [hl] ; load trainer mon set
  2358. ld [wEngagedTrainerSet], a
  2359. jp PlayTrainerMusic
  2360. PrintEndBattleText::
  2361. push hl
  2362. ld hl, wd72d
  2363. bit 7, [hl]
  2364. res 7, [hl]
  2365. pop hl
  2366. ret z
  2367. ld a, [H_LOADEDROMBANK]
  2368. push af
  2369. ld a, [wEndBattleTextRomBank]
  2370. ld [H_LOADEDROMBANK], a
  2371. ld [MBC1RomBank], a
  2372. push hl
  2373. callba SaveTrainerName
  2374. ld hl, TrainerEndBattleText
  2375. call PrintText
  2376. pop hl
  2377. pop af
  2378. ld [H_LOADEDROMBANK], a
  2379. ld [MBC1RomBank], a
  2380. callba FreezeEnemyTrainerSprite
  2381. jp WaitForSoundToFinish
  2382. GetSavedEndBattleTextPointer::
  2383. ld a, [wBattleResult]
  2384. and a
  2385. ; won battle
  2386. jr nz, .lostBattle
  2387. ld a, [wEndBattleWinTextPointer]
  2388. ld h, a
  2389. ld a, [wEndBattleWinTextPointer + 1]
  2390. ld l, a
  2391. ret
  2392. .lostBattle
  2393. ld a, [wEndBattleLoseTextPointer]
  2394. ld h, a
  2395. ld a, [wEndBattleLoseTextPointer + 1]
  2396. ld l, a
  2397. ret
  2398. TrainerEndBattleText::
  2399. TX_FAR _TrainerNameText
  2400. TX_ASM
  2401. call GetSavedEndBattleTextPointer
  2402. call TextCommandProcessor
  2403. jp TextScriptEnd
  2404. ; only engage withe trainer if the player is not already
  2405. ; engaged with another trainer
  2406. ; XXX unused?
  2407. CheckIfAlreadyEngaged::
  2408. ld a, [wFlags_0xcd60]
  2409. bit 0, a
  2410. ret nz
  2411. call EngageMapTrainer
  2412. xor a
  2413. ret
  2414. PlayTrainerMusic::
  2415. ld a, [wEngagedTrainerClass]
  2416. cp OPP_SONY1
  2417. ret z
  2418. cp OPP_SONY2
  2419. ret z
  2420. cp OPP_SONY3
  2421. ret z
  2422. ld a, [wGymLeaderNo]
  2423. and a
  2424. ret nz
  2425. xor a
  2426. ld [wAudioFadeOutControl], a
  2427. ld a, $ff
  2428. call PlaySound
  2429. ld a, BANK(Music_MeetEvilTrainer)
  2430. ld [wAudioROMBank], a
  2431. ld [wAudioSavedROMBank], a
  2432. ld a, [wEngagedTrainerClass]
  2433. ld b, a
  2434. ld hl, EvilTrainerList
  2435. .evilTrainerListLoop
  2436. ld a, [hli]
  2437. cp $ff
  2438. jr z, .noEvilTrainer
  2439. cp b
  2440. jr nz, .evilTrainerListLoop
  2441. ld a, MUSIC_MEET_EVIL_TRAINER
  2442. jr .PlaySound
  2443. .noEvilTrainer
  2444. ld hl, FemaleTrainerList
  2445. .femaleTrainerListLoop
  2446. ld a, [hli]
  2447. cp $ff
  2448. jr z, .maleTrainer
  2449. cp b
  2450. jr nz, .femaleTrainerListLoop
  2451. ld a, MUSIC_MEET_FEMALE_TRAINER
  2452. jr .PlaySound
  2453. .maleTrainer
  2454. ld a, MUSIC_MEET_MALE_TRAINER
  2455. .PlaySound
  2456. ld [wNewSoundID], a
  2457. jp PlaySound
  2458. INCLUDE "data/trainer_types.asm"
  2459. ; checks if the player's coordinates match an arrow movement tile's coordinates
  2460. ; and if so, decodes the RLE movement data
  2461. ; b = player Y
  2462. ; c = player X
  2463. DecodeArrowMovementRLE::
  2464. ld a, [hli]
  2465. cp $ff
  2466. ret z ; no match in the list
  2467. cp b
  2468. jr nz, .nextArrowMovementTileEntry1
  2469. ld a, [hli]
  2470. cp c
  2471. jr nz, .nextArrowMovementTileEntry2
  2472. ld a, [hli]
  2473. ld d, [hl]
  2474. ld e, a
  2475. ld hl, wSimulatedJoypadStatesEnd
  2476. call DecodeRLEList
  2477. dec a
  2478. ld [wSimulatedJoypadStatesIndex], a
  2479. ret
  2480. .nextArrowMovementTileEntry1
  2481. inc hl
  2482. .nextArrowMovementTileEntry2
  2483. inc hl
  2484. inc hl
  2485. jr DecodeArrowMovementRLE
  2486. FuncTX_ItemStoragePC::
  2487. call SaveScreenTilesToBuffer2
  2488. ld b, BANK(PlayerPC)
  2489. ld hl, PlayerPC
  2490. jr bankswitchAndContinue
  2491. FuncTX_BillsPC::
  2492. call SaveScreenTilesToBuffer2
  2493. ld b, BANK(BillsPC_)
  2494. ld hl, BillsPC_
  2495. jr bankswitchAndContinue
  2496. FuncTX_GameCornerPrizeMenu::
  2497. ; XXX find a better name for this function
  2498. ; special_F7
  2499. ld b, BANK(CeladonPrizeMenu)
  2500. ld hl, CeladonPrizeMenu
  2501. bankswitchAndContinue::
  2502. call Bankswitch
  2503. jp HoldTextDisplayOpen ; continue to main text-engine function
  2504. FuncTX_PokemonCenterPC::
  2505. ld b, BANK(ActivatePC)
  2506. ld hl, ActivatePC
  2507. jr bankswitchAndContinue
  2508. StartSimulatingJoypadStates::
  2509. xor a
  2510. ld [wOverrideSimulatedJoypadStatesMask], a
  2511. ld [wSpriteStateData2 + $06], a ; player's sprite movement byte 1
  2512. ld hl, wd730
  2513. set 7, [hl]
  2514. ret
  2515. IsItemInBag::
  2516. ; given an item_id in b
  2517. ; set zero flag if item isn't in player's bag
  2518. ; else reset zero flag
  2519. ; related to Pokémon Tower and ghosts
  2520. predef GetQuantityOfItemInBag
  2521. ld a, b
  2522. and a
  2523. ret
  2524. DisplayPokedex::
  2525. ld [wd11e], a
  2526. jpba _DisplayPokedex
  2527. SetSpriteFacingDirectionAndDelay::
  2528. call SetSpriteFacingDirection
  2529. ld c, 6
  2530. jp DelayFrames
  2531. SetSpriteFacingDirection::
  2532. ld a, $9
  2533. ld [H_SPRITEDATAOFFSET], a
  2534. call GetPointerWithinSpriteStateData1
  2535. ld a, [hSpriteFacingDirection]
  2536. ld [hl], a
  2537. ret
  2538. SetSpriteImageIndexAfterSettingFacingDirection::
  2539. ld de, -7
  2540. add hl, de
  2541. ld [hl], a
  2542. ret
  2543. ; tests if the player's coordinates are in a specified array
  2544. ; INPUT:
  2545. ; hl = address of array
  2546. ; OUTPUT:
  2547. ; [wCoordIndex] = if there is match, the matching array index
  2548. ; sets carry if the coordinates are in the array, clears carry if not
  2549. ArePlayerCoordsInArray::
  2550. ld a, [wYCoord]
  2551. ld b, a
  2552. ld a, [wXCoord]
  2553. ld c, a
  2554. ; fallthrough
  2555. CheckCoords::
  2556. xor a
  2557. ld [wCoordIndex], a
  2558. .loop
  2559. ld a, [hli]
  2560. cp $ff ; reached terminator?
  2561. jr z, .notInArray
  2562. push hl
  2563. ld hl, wCoordIndex
  2564. inc [hl]
  2565. pop hl
  2566. .compareYCoord
  2567. cp b
  2568. jr z, .compareXCoord
  2569. inc hl
  2570. jr .loop
  2571. .compareXCoord
  2572. ld a, [hli]
  2573. cp c
  2574. jr nz, .loop
  2575. .inArray
  2576. scf
  2577. ret
  2578. .notInArray
  2579. and a
  2580. ret
  2581. ; tests if a boulder's coordinates are in a specified array
  2582. ; INPUT:
  2583. ; hl = address of array
  2584. ; [H_SPRITEINDEX] = index of boulder sprite
  2585. ; OUTPUT:
  2586. ; [wCoordIndex] = if there is match, the matching array index
  2587. ; sets carry if the coordinates are in the array, clears carry if not
  2588. CheckBoulderCoords::
  2589. push hl
  2590. ld hl, wSpriteStateData2 + $04
  2591. ld a, [H_SPRITEINDEX]
  2592. swap a
  2593. ld d, $0
  2594. ld e, a
  2595. add hl, de
  2596. ld a, [hli]
  2597. sub $4 ; because sprite coordinates are offset by 4
  2598. ld b, a
  2599. ld a, [hl]
  2600. sub $4 ; because sprite coordinates are offset by 4
  2601. ld c, a
  2602. pop hl
  2603. jp CheckCoords
  2604. GetPointerWithinSpriteStateData1::
  2605. ld h, $c1
  2606. jr _GetPointerWithinSpriteStateData
  2607. GetPointerWithinSpriteStateData2::
  2608. ld h, $c2
  2609. _GetPointerWithinSpriteStateData:
  2610. ld a, [H_SPRITEDATAOFFSET]
  2611. ld b, a
  2612. ld a, [H_SPRITEINDEX]
  2613. swap a
  2614. add b
  2615. ld l, a
  2616. ret
  2617. ; decodes a $ff-terminated RLEncoded list
  2618. ; each entry is a pair of bytes <byte value> <repetitions>
  2619. ; the final $ff will be replicated in the output list and a contains the number of bytes written
  2620. ; de: input list
  2621. ; hl: output list
  2622. DecodeRLEList::
  2623. xor a
  2624. ld [wRLEByteCount], a ; count written bytes here
  2625. .listLoop
  2626. ld a, [de]
  2627. cp $ff
  2628. jr z, .endOfList
  2629. ld [hRLEByteValue], a ; store byte value to be written
  2630. inc de
  2631. ld a, [de]
  2632. ld b, $0
  2633. ld c, a ; number of bytes to be written
  2634. ld a, [wRLEByteCount]
  2635. add c
  2636. ld [wRLEByteCount], a ; update total number of written bytes
  2637. ld a, [hRLEByteValue]
  2638. call FillMemory ; write a c-times to output
  2639. inc de
  2640. jr .listLoop
  2641. .endOfList
  2642. ld a, $ff
  2643. ld [hl], a ; write final $ff
  2644. ld a, [wRLEByteCount]
  2645. inc a ; include sentinel in counting
  2646. ret
  2647. ; sets movement byte 1 for sprite [H_SPRITEINDEX] to $FE and byte 2 to [hSpriteMovementByte2]
  2648. SetSpriteMovementBytesToFE::
  2649. push hl
  2650. call GetSpriteMovementByte1Pointer
  2651. ld [hl], $fe
  2652. call GetSpriteMovementByte2Pointer
  2653. ld a, [hSpriteMovementByte2]
  2654. ld [hl], a
  2655. pop hl
  2656. ret
  2657. ; sets both movement bytes for sprite [H_SPRITEINDEX] to $FF
  2658. SetSpriteMovementBytesToFF::
  2659. push hl
  2660. call GetSpriteMovementByte1Pointer
  2661. ld [hl], $FF
  2662. call GetSpriteMovementByte2Pointer
  2663. ld [hl], $FF ; prevent person from walking?
  2664. pop hl
  2665. ret
  2666. ; returns the sprite movement byte 1 pointer for sprite [H_SPRITEINDEX] in hl
  2667. GetSpriteMovementByte1Pointer::
  2668. ld h, $C2
  2669. ld a, [H_SPRITEINDEX]
  2670. swap a
  2671. add 6
  2672. ld l, a
  2673. ret
  2674. ; returns the sprite movement byte 2 pointer for sprite [H_SPRITEINDEX] in hl
  2675. GetSpriteMovementByte2Pointer::
  2676. push de
  2677. ld hl, wMapSpriteData
  2678. ld a, [H_SPRITEINDEX]
  2679. dec a
  2680. add a
  2681. ld d, 0
  2682. ld e, a
  2683. add hl, de
  2684. pop de
  2685. ret
  2686. GetTrainerInformation::
  2687. call GetTrainerName
  2688. ld a, [wLinkState]
  2689. and a
  2690. jr nz, .linkBattle
  2691. ld a, Bank(TrainerPicAndMoneyPointers)
  2692. call BankswitchHome
  2693. ld a, [wTrainerClass]
  2694. dec a
  2695. ld hl, TrainerPicAndMoneyPointers
  2696. ld bc, $5
  2697. call AddNTimes
  2698. ld de, wTrainerPicPointer
  2699. ld a, [hli]
  2700. ld [de], a
  2701. inc de
  2702. ld a, [hli]
  2703. ld [de], a
  2704. ld de, wTrainerBaseMoney
  2705. ld a, [hli]
  2706. ld [de], a
  2707. inc de
  2708. ld a, [hli]
  2709. ld [de], a
  2710. jp BankswitchBack
  2711. .linkBattle
  2712. ld hl, wTrainerPicPointer
  2713. ld de, RedPicFront
  2714. ld [hl], e
  2715. inc hl
  2716. ld [hl], d
  2717. ret
  2718. GetTrainerName::
  2719. jpba GetTrainerName_
  2720. HasEnoughMoney::
  2721. ; Check if the player has at least as much
  2722. ; money as the 3-byte BCD value at hMoney.
  2723. ld de, wPlayerMoney
  2724. ld hl, hMoney
  2725. ld c, 3
  2726. jp StringCmp
  2727. HasEnoughCoins::
  2728. ; Check if the player has at least as many
  2729. ; coins as the 2-byte BCD value at hCoins.
  2730. ld de, wPlayerCoins
  2731. ld hl, hCoins
  2732. ld c, 2
  2733. jp StringCmp
  2734. BankswitchHome::
  2735. ; switches to bank # in a
  2736. ; Only use this when in the home bank!
  2737. ld [wBankswitchHomeTemp], a
  2738. ld a, [H_LOADEDROMBANK]
  2739. ld [wBankswitchHomeSavedROMBank], a
  2740. ld a, [wBankswitchHomeTemp]
  2741. ld [H_LOADEDROMBANK], a
  2742. ld [MBC1RomBank], a
  2743. ret
  2744. BankswitchBack::
  2745. ; returns from BankswitchHome
  2746. ld a, [wBankswitchHomeSavedROMBank]
  2747. ld [H_LOADEDROMBANK], a
  2748. ld [MBC1RomBank], a
  2749. ret
  2750. Bankswitch::
  2751. ; self-contained bankswitch, use this when not in the home bank
  2752. ; switches to the bank in b
  2753. ld a, [H_LOADEDROMBANK]
  2754. push af
  2755. ld a, b
  2756. ld [H_LOADEDROMBANK], a
  2757. ld [MBC1RomBank], a
  2758. ld bc, .Return
  2759. push bc
  2760. jp hl
  2761. .Return
  2762. pop bc
  2763. ld a, b
  2764. ld [H_LOADEDROMBANK], a
  2765. ld [MBC1RomBank], a
  2766. ret
  2767. ; displays yes/no choice
  2768. ; yes -> set carry
  2769. YesNoChoice::
  2770. call SaveScreenTilesToBuffer1
  2771. call InitYesNoTextBoxParameters
  2772. jr DisplayYesNoChoice
  2773. Func_35f4::
  2774. ld a, TWO_OPTION_MENU
  2775. ld [wTextBoxID], a
  2776. call InitYesNoTextBoxParameters
  2777. jp DisplayTextBoxID
  2778. InitYesNoTextBoxParameters::
  2779. xor a ; YES_NO_MENU
  2780. ld [wTwoOptionMenuID], a
  2781. coord hl, 14, 7
  2782. ld bc, $80f
  2783. ret
  2784. YesNoChoicePokeCenter::
  2785. call SaveScreenTilesToBuffer1
  2786. ld a, HEAL_CANCEL_MENU
  2787. ld [wTwoOptionMenuID], a
  2788. coord hl, 11, 6
  2789. lb bc, 8, 12
  2790. jr DisplayYesNoChoice
  2791. WideYesNoChoice:: ; unused
  2792. call SaveScreenTilesToBuffer1
  2793. ld a, WIDE_YES_NO_MENU
  2794. ld [wTwoOptionMenuID], a
  2795. coord hl, 12, 7
  2796. lb bc, 8, 13
  2797. DisplayYesNoChoice::
  2798. ld a, TWO_OPTION_MENU
  2799. ld [wTextBoxID], a
  2800. call DisplayTextBoxID
  2801. jp LoadScreenTilesFromBuffer1
  2802. ; calculates the difference |a-b|, setting carry flag if a<b
  2803. CalcDifference::
  2804. sub b
  2805. ret nc
  2806. cpl
  2807. add $1
  2808. scf
  2809. ret
  2810. MoveSprite::
  2811. ; move the sprite [H_SPRITEINDEX] with the movement pointed to by de
  2812. ; actually only copies the movement data to wNPCMovementDirections for later
  2813. call SetSpriteMovementBytesToFF
  2814. MoveSprite_::
  2815. push hl
  2816. push bc
  2817. call GetSpriteMovementByte1Pointer
  2818. xor a
  2819. ld [hl], a
  2820. ld hl, wNPCMovementDirections
  2821. ld c, 0
  2822. .loop
  2823. ld a, [de]
  2824. ld [hli], a
  2825. inc de
  2826. inc c
  2827. cp $FF ; have we reached the end of the movement data?
  2828. jr nz, .loop
  2829. ld a, c
  2830. ld [wNPCNumScriptedSteps], a ; number of steps taken
  2831. pop bc
  2832. ld hl, wd730
  2833. set 0, [hl]
  2834. pop hl
  2835. xor a
  2836. ld [wOverrideSimulatedJoypadStatesMask], a
  2837. ld [wSimulatedJoypadStatesEnd], a
  2838. dec a
  2839. ld [wJoyIgnore], a
  2840. ld [wWastedByteCD3A], a
  2841. ret
  2842. ; divides [hDividend2] by [hDivisor2] and stores the quotient in [hQuotient2]
  2843. DivideBytes::
  2844. push hl
  2845. ld hl, hQuotient2
  2846. xor a
  2847. ld [hld], a
  2848. ld a, [hld]
  2849. and a
  2850. jr z, .done
  2851. ld a, [hli]
  2852. .loop
  2853. sub [hl]
  2854. jr c, .done
  2855. inc hl
  2856. inc [hl]
  2857. dec hl
  2858. jr .loop
  2859. .done
  2860. pop hl
  2861. ret
  2862. LoadFontTilePatterns::
  2863. ld a, [rLCDC]
  2864. bit 7, a ; is the LCD enabled?
  2865. jr nz, .on
  2866. .off
  2867. ld hl, FontGraphics
  2868. ld de, vFont
  2869. ld bc, FontGraphicsEnd - FontGraphics
  2870. ld a, BANK(FontGraphics)
  2871. jp FarCopyDataDouble ; if LCD is off, transfer all at once
  2872. .on
  2873. ld de, FontGraphics
  2874. ld hl, vFont
  2875. lb bc, BANK(FontGraphics), (FontGraphicsEnd - FontGraphics) / $8
  2876. jp CopyVideoDataDouble ; if LCD is on, transfer during V-blank
  2877. LoadTextBoxTilePatterns::
  2878. ld a, [rLCDC]
  2879. bit 7, a ; is the LCD enabled?
  2880. jr nz, .on
  2881. .off
  2882. ld hl, TextBoxGraphics
  2883. ld de, vChars2 + $600
  2884. ld bc, TextBoxGraphicsEnd - TextBoxGraphics
  2885. ld a, BANK(TextBoxGraphics)
  2886. jp FarCopyData2 ; if LCD is off, transfer all at once
  2887. .on
  2888. ld de, TextBoxGraphics
  2889. ld hl, vChars2 + $600
  2890. lb bc, BANK(TextBoxGraphics), (TextBoxGraphicsEnd - TextBoxGraphics) / $10
  2891. jp CopyVideoData ; if LCD is on, transfer during V-blank
  2892. LoadHpBarAndStatusTilePatterns::
  2893. ld a, [rLCDC]
  2894. bit 7, a ; is the LCD enabled?
  2895. jr nz, .on
  2896. .off
  2897. ld hl, HpBarAndStatusGraphics
  2898. ld de, vChars2 + $620
  2899. ld bc, HpBarAndStatusGraphicsEnd - HpBarAndStatusGraphics
  2900. ld a, BANK(HpBarAndStatusGraphics)
  2901. jp FarCopyData2 ; if LCD is off, transfer all at once
  2902. .on
  2903. ld de, HpBarAndStatusGraphics
  2904. ld hl, vChars2 + $620
  2905. lb bc, BANK(HpBarAndStatusGraphics), (HpBarAndStatusGraphicsEnd - HpBarAndStatusGraphics) / $10
  2906. jp CopyVideoData ; if LCD is on, transfer during V-blank
  2907. FillMemory::
  2908. ; Fill bc bytes at hl with a.
  2909. push de
  2910. ld d, a
  2911. .loop
  2912. ld a, d
  2913. ld [hli], a
  2914. dec bc
  2915. ld a, b
  2916. or c
  2917. jr nz, .loop
  2918. pop de
  2919. ret
  2920. UncompressSpriteFromDE::
  2921. ; Decompress pic at a:de.
  2922. ld hl, wSpriteInputPtr
  2923. ld [hl], e
  2924. inc hl
  2925. ld [hl], d
  2926. jp UncompressSpriteData
  2927. SaveScreenTilesToBuffer2::
  2928. coord hl, 0, 0
  2929. ld de, wTileMapBackup2
  2930. ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
  2931. call CopyData
  2932. ret
  2933. LoadScreenTilesFromBuffer2::
  2934. call LoadScreenTilesFromBuffer2DisableBGTransfer
  2935. ld a, 1
  2936. ld [H_AUTOBGTRANSFERENABLED], a
  2937. ret
  2938. ; loads screen tiles stored in wTileMapBackup2 but leaves H_AUTOBGTRANSFERENABLED disabled
  2939. LoadScreenTilesFromBuffer2DisableBGTransfer::
  2940. xor a
  2941. ld [H_AUTOBGTRANSFERENABLED], a
  2942. ld hl, wTileMapBackup2
  2943. coord de, 0, 0
  2944. ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
  2945. call CopyData
  2946. ret
  2947. SaveScreenTilesToBuffer1::
  2948. coord hl, 0, 0
  2949. ld de, wTileMapBackup
  2950. ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
  2951. jp CopyData
  2952. LoadScreenTilesFromBuffer1::
  2953. xor a
  2954. ld [H_AUTOBGTRANSFERENABLED], a
  2955. ld hl, wTileMapBackup
  2956. coord de, 0, 0
  2957. ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
  2958. call CopyData
  2959. ld a, 1
  2960. ld [H_AUTOBGTRANSFERENABLED], a
  2961. ret
  2962. DelayFrames::
  2963. ; wait c frames
  2964. call DelayFrame
  2965. dec c
  2966. jr nz, DelayFrames
  2967. ret
  2968. PlaySoundWaitForCurrent::
  2969. push af
  2970. call WaitForSoundToFinish
  2971. pop af
  2972. jp PlaySound
  2973. ; Wait for sound to finish playing
  2974. WaitForSoundToFinish::
  2975. ld a, [wLowHealthAlarm]
  2976. and $80
  2977. ret nz
  2978. push hl
  2979. .waitLoop
  2980. ld hl, wChannelSoundIDs + Ch4
  2981. xor a
  2982. or [hl]
  2983. inc hl
  2984. or [hl]
  2985. inc hl
  2986. inc hl
  2987. or [hl]
  2988. jr nz, .waitLoop
  2989. pop hl
  2990. ret
  2991. NamePointers::
  2992. dw MonsterNames
  2993. dw MoveNames
  2994. dw UnusedNames
  2995. dw ItemNames
  2996. dw wPartyMonOT ; player's OT names list
  2997. dw wEnemyMonOT ; enemy's OT names list
  2998. dw TrainerNames
  2999. GetName::
  3000. ; arguments:
  3001. ; [wd0b5] = which name
  3002. ; [wNameListType] = which list
  3003. ; [wPredefBank] = bank of list
  3004. ;
  3005. ; returns pointer to name in de
  3006. ld a, [wd0b5]
  3007. ld [wd11e], a
  3008. ; TM names are separate from item names.
  3009. ; BUG: This applies to all names instead of just items.
  3010. cp HM_01
  3011. jp nc, GetMachineName
  3012. ld a, [H_LOADEDROMBANK]
  3013. push af
  3014. push hl
  3015. push bc
  3016. push de
  3017. ld a, [wNameListType] ;List3759_entrySelector
  3018. dec a
  3019. jr nz, .otherEntries
  3020. ;1 = MON_NAMES
  3021. call GetMonName
  3022. ld hl, NAME_LENGTH
  3023. add hl, de
  3024. ld e, l
  3025. ld d, h
  3026. jr .gotPtr
  3027. .otherEntries
  3028. ;2-7 = OTHER ENTRIES
  3029. ld a, [wPredefBank]
  3030. ld [H_LOADEDROMBANK], a
  3031. ld [MBC1RomBank], a
  3032. ld a, [wNameListType] ;VariousNames' entryID
  3033. dec a
  3034. add a
  3035. ld d, 0
  3036. ld e, a
  3037. jr nc, .skip
  3038. inc d
  3039. .skip
  3040. ld hl, NamePointers
  3041. add hl, de
  3042. ld a, [hli]
  3043. ld [$ff96], a
  3044. ld a, [hl]
  3045. ld [$ff95], a
  3046. ld a, [$ff95]
  3047. ld h, a
  3048. ld a, [$ff96]
  3049. ld l, a
  3050. ld a, [wd0b5]
  3051. ld b, a
  3052. ld c, 0
  3053. .nextName
  3054. ld d, h
  3055. ld e, l
  3056. .nextChar
  3057. ld a, [hli]
  3058. cp "@"
  3059. jr nz, .nextChar
  3060. inc c ;entry counter
  3061. ld a, b ;wanted entry
  3062. cp c
  3063. jr nz, .nextName
  3064. ld h, d
  3065. ld l, e
  3066. ld de, wcd6d
  3067. ld bc, $0014
  3068. call CopyData
  3069. .gotPtr
  3070. ld a, e
  3071. ld [wUnusedCF8D], a
  3072. ld a, d
  3073. ld [wUnusedCF8D + 1], a
  3074. pop de
  3075. pop bc
  3076. pop hl
  3077. pop af
  3078. ld [H_LOADEDROMBANK], a
  3079. ld [MBC1RomBank], a
  3080. ret
  3081. GetItemPrice::
  3082. ; Stores item's price as BCD at hItemPrice (3 bytes)
  3083. ; Input: [wcf91] = item id
  3084. ld a, [H_LOADEDROMBANK]
  3085. push af
  3086. ld a, [wListMenuID]
  3087. cp MOVESLISTMENU
  3088. ld a, BANK(ItemPrices)
  3089. jr nz, .ok
  3090. ld a, $f ; hardcoded Bank
  3091. .ok
  3092. ld [H_LOADEDROMBANK], a
  3093. ld [MBC1RomBank], a
  3094. ld hl, wItemPrices
  3095. ld a, [hli]
  3096. ld h, [hl]
  3097. ld l, a
  3098. ld a, [wcf91] ; a contains item id
  3099. cp HM_01
  3100. jr nc, .getTMPrice
  3101. ld bc, $3
  3102. .loop
  3103. add hl, bc
  3104. dec a
  3105. jr nz, .loop
  3106. dec hl
  3107. ld a, [hld]
  3108. ld [hItemPrice + 2], a
  3109. ld a, [hld]
  3110. ld [hItemPrice + 1], a
  3111. ld a, [hl]
  3112. ld [hItemPrice], a
  3113. jr .done
  3114. .getTMPrice
  3115. ld a, Bank(GetMachinePrice)
  3116. ld [H_LOADEDROMBANK], a
  3117. ld [MBC1RomBank], a
  3118. call GetMachinePrice
  3119. .done
  3120. ld de, hItemPrice
  3121. pop af
  3122. ld [H_LOADEDROMBANK], a
  3123. ld [MBC1RomBank], a
  3124. ret
  3125. ; copies a string from [de] to [wcf4b]
  3126. CopyStringToCF4B::
  3127. ld hl, wcf4b
  3128. ; fall through
  3129. ; copies a string from [de] to [hl]
  3130. CopyString::
  3131. ld a, [de]
  3132. inc de
  3133. ld [hli], a
  3134. cp "@"
  3135. jr nz, CopyString
  3136. ret
  3137. ; this function is used when lower button sensitivity is wanted (e.g. menus)
  3138. ; OUTPUT: [hJoy5] = pressed buttons in usual format
  3139. ; there are two flags that control its functionality, [hJoy6] and [hJoy7]
  3140. ; there are essentially three modes of operation
  3141. ; 1. Get newly pressed buttons only
  3142. ; ([hJoy7] == 0, [hJoy6] == any)
  3143. ; Just copies [hJoyPressed] to [hJoy5].
  3144. ; 2. Get currently pressed buttons at low sample rate with delay
  3145. ; ([hJoy7] == 1, [hJoy6] != 0)
  3146. ; If the user holds down buttons for more than half a second,
  3147. ; report buttons as being pressed up to 12 times per second thereafter.
  3148. ; If the user holds down buttons for less than half a second,
  3149. ; report only one button press.
  3150. ; 3. Same as 2, but report no buttons as pressed if A or B is held down.
  3151. ; ([hJoy7] == 1, [hJoy6] == 0)
  3152. JoypadLowSensitivity::
  3153. call Joypad
  3154. ld a, [hJoy7] ; flag
  3155. and a ; get all currently pressed buttons or only newly pressed buttons?
  3156. ld a, [hJoyPressed] ; newly pressed buttons
  3157. jr z, .storeButtonState
  3158. ld a, [hJoyHeld] ; all currently pressed buttons
  3159. .storeButtonState
  3160. ld [hJoy5], a
  3161. ld a, [hJoyPressed] ; newly pressed buttons
  3162. and a ; have any buttons been newly pressed since last check?
  3163. jr z, .noNewlyPressedButtons
  3164. .newlyPressedButtons
  3165. ld a, 30 ; half a second delay
  3166. ld [H_FRAMECOUNTER], a
  3167. ret
  3168. .noNewlyPressedButtons
  3169. ld a, [H_FRAMECOUNTER]
  3170. and a ; is the delay over?
  3171. jr z, .delayOver
  3172. .delayNotOver
  3173. xor a
  3174. ld [hJoy5], a ; report no buttons as pressed
  3175. ret
  3176. .delayOver
  3177. ; if [hJoy6] = 0 and A or B is pressed, report no buttons as pressed
  3178. ld a, [hJoyHeld]
  3179. and A_BUTTON | B_BUTTON
  3180. jr z, .setShortDelay
  3181. ld a, [hJoy6] ; flag
  3182. and a
  3183. jr nz, .setShortDelay
  3184. xor a
  3185. ld [hJoy5], a
  3186. .setShortDelay
  3187. ld a, 5 ; 1/12 of a second delay
  3188. ld [H_FRAMECOUNTER], a
  3189. ret
  3190. WaitForTextScrollButtonPress::
  3191. ld a, [H_DOWNARROWBLINKCNT1]
  3192. push af
  3193. ld a, [H_DOWNARROWBLINKCNT2]
  3194. push af
  3195. xor a
  3196. ld [H_DOWNARROWBLINKCNT1], a
  3197. ld a, $6
  3198. ld [H_DOWNARROWBLINKCNT2], a
  3199. .loop
  3200. push hl
  3201. ld a, [wTownMapSpriteBlinkingEnabled]
  3202. and a
  3203. jr z, .skipAnimation
  3204. call TownMapSpriteBlinkingAnimation
  3205. .skipAnimation
  3206. coord hl, 18, 16
  3207. call HandleDownArrowBlinkTiming
  3208. pop hl
  3209. call JoypadLowSensitivity
  3210. predef CableClub_Run
  3211. ld a, [hJoy5]
  3212. and A_BUTTON | B_BUTTON
  3213. jr z, .loop
  3214. pop af
  3215. ld [H_DOWNARROWBLINKCNT2], a
  3216. pop af
  3217. ld [H_DOWNARROWBLINKCNT1], a
  3218. ret
  3219. ; (unless in link battle) waits for A or B being pressed and outputs the scrolling sound effect
  3220. ManualTextScroll::
  3221. ld a, [wLinkState]
  3222. cp LINK_STATE_BATTLING
  3223. jr z, .inLinkBattle
  3224. call WaitForTextScrollButtonPress
  3225. ld a, SFX_PRESS_AB
  3226. jp PlaySound
  3227. .inLinkBattle
  3228. ld c, 65
  3229. jp DelayFrames
  3230. ; function to do multiplication
  3231. ; all values are big endian
  3232. ; INPUT
  3233. ; FF96-FF98 = multiplicand
  3234. ; FF99 = multiplier
  3235. ; OUTPUT
  3236. ; FF95-FF98 = product
  3237. Multiply::
  3238. push hl
  3239. push bc
  3240. callab _Multiply
  3241. pop bc
  3242. pop hl
  3243. ret
  3244. ; function to do division
  3245. ; all values are big endian
  3246. ; INPUT
  3247. ; FF95-FF98 = dividend
  3248. ; FF99 = divisor
  3249. ; b = number of bytes in the dividend (starting from FF95)
  3250. ; OUTPUT
  3251. ; FF95-FF98 = quotient
  3252. ; FF99 = remainder
  3253. Divide::
  3254. push hl
  3255. push de
  3256. push bc
  3257. ld a, [H_LOADEDROMBANK]
  3258. push af
  3259. ld a, Bank(_Divide)
  3260. ld [H_LOADEDROMBANK], a
  3261. ld [MBC1RomBank], a
  3262. call _Divide
  3263. pop af
  3264. ld [H_LOADEDROMBANK], a
  3265. ld [MBC1RomBank], a
  3266. pop bc
  3267. pop de
  3268. pop hl
  3269. ret
  3270. ; This function is used to wait a short period after printing a letter to the
  3271. ; screen unless the player presses the A/B button or the delay is turned off
  3272. ; through the [wd730] or [wLetterPrintingDelayFlags] flags.
  3273. PrintLetterDelay::
  3274. ld a, [wd730]
  3275. bit 6, a
  3276. ret nz
  3277. ld a, [wLetterPrintingDelayFlags]
  3278. bit 1, a
  3279. ret z
  3280. push hl
  3281. push de
  3282. push bc
  3283. ld a, [wLetterPrintingDelayFlags]
  3284. bit 0, a
  3285. jr z, .waitOneFrame
  3286. ld a, [wOptions]
  3287. and $f
  3288. ld [H_FRAMECOUNTER], a
  3289. jr .checkButtons
  3290. .waitOneFrame
  3291. ld a, 1
  3292. ld [H_FRAMECOUNTER], a
  3293. .checkButtons
  3294. call Joypad
  3295. ld a, [hJoyHeld]
  3296. .checkAButton
  3297. bit 0, a ; is the A button pressed?
  3298. jr z, .checkBButton
  3299. jr .endWait
  3300. .checkBButton
  3301. bit 1, a ; is the B button pressed?
  3302. jr z, .buttonsNotPressed
  3303. .endWait
  3304. call DelayFrame
  3305. jr .done
  3306. .buttonsNotPressed ; if neither A nor B is pressed
  3307. ld a, [H_FRAMECOUNTER]
  3308. and a
  3309. jr nz, .checkButtons
  3310. .done
  3311. pop bc
  3312. pop de
  3313. pop hl
  3314. ret
  3315. ; Copies [hl, bc) to [de, bc - hl).
  3316. ; In other words, the source data is from hl up to but not including bc,
  3317. ; and the destination is de.
  3318. CopyDataUntil::
  3319. ld a, [hli]
  3320. ld [de], a
  3321. inc de
  3322. ld a, h
  3323. cp b
  3324. jr nz, CopyDataUntil
  3325. ld a, l
  3326. cp c
  3327. jr nz, CopyDataUntil
  3328. ret
  3329. ; Function to remove a pokemon from the party or the current box.
  3330. ; wWhichPokemon determines the pokemon.
  3331. ; [wRemoveMonFromBox] == 0 specifies the party.
  3332. ; [wRemoveMonFromBox] != 0 specifies the current box.
  3333. RemovePokemon::
  3334. jpab _RemovePokemon
  3335. AddPartyMon::
  3336. push hl
  3337. push de
  3338. push bc
  3339. callba _AddPartyMon
  3340. pop bc
  3341. pop de
  3342. pop hl
  3343. ret
  3344. ; calculates all 5 stats of current mon and writes them to [de]
  3345. CalcStats::
  3346. ld c, $0
  3347. .statsLoop
  3348. inc c
  3349. call CalcStat
  3350. ld a, [H_MULTIPLICAND+1]
  3351. ld [de], a
  3352. inc de
  3353. ld a, [H_MULTIPLICAND+2]
  3354. ld [de], a
  3355. inc de
  3356. ld a, c
  3357. cp NUM_STATS
  3358. jr nz, .statsLoop
  3359. ret
  3360. ; calculates stat c of current mon
  3361. ; c: stat to calc (HP=1,Atk=2,Def=3,Spd=4,Spc=5)
  3362. ; b: consider stat exp?
  3363. ; hl: base ptr to stat exp values ([hl + 2*c - 1] and [hl + 2*c])
  3364. CalcStat::
  3365. push hl
  3366. push de
  3367. push bc
  3368. ld a, b
  3369. ld d, a
  3370. push hl
  3371. ld hl, wMonHeader
  3372. ld b, $0
  3373. add hl, bc
  3374. ld a, [hl] ; read base value of stat
  3375. ld e, a
  3376. pop hl
  3377. push hl
  3378. sla c
  3379. ld a, d
  3380. and a
  3381. jr z, .statExpDone ; consider stat exp?
  3382. add hl, bc ; skip to corresponding stat exp value
  3383. .statExpLoop ; calculates ceil(Sqrt(stat exp)) in b
  3384. xor a
  3385. ld [H_MULTIPLICAND], a
  3386. ld [H_MULTIPLICAND+1], a
  3387. inc b ; increment current stat exp bonus
  3388. ld a, b
  3389. cp $ff
  3390. jr z, .statExpDone
  3391. ld [H_MULTIPLICAND+2], a
  3392. ld [H_MULTIPLIER], a
  3393. call Multiply
  3394. ld a, [hld]
  3395. ld d, a
  3396. ld a, [$ff98]
  3397. sub d
  3398. ld a, [hli]
  3399. ld d, a
  3400. ld a, [$ff97]
  3401. sbc d ; test if (current stat exp bonus)^2 < stat exp
  3402. jr c, .statExpLoop
  3403. .statExpDone
  3404. srl c
  3405. pop hl
  3406. push bc
  3407. ld bc, wPartyMon1DVs - (wPartyMon1HPExp - 1) ; also wEnemyMonDVs - wEnemyMonHP
  3408. add hl, bc
  3409. pop bc
  3410. ld a, c
  3411. cp $2
  3412. jr z, .getAttackIV
  3413. cp $3
  3414. jr z, .getDefenseIV
  3415. cp $4
  3416. jr z, .getSpeedIV
  3417. cp $5
  3418. jr z, .getSpecialIV
  3419. .getHpIV
  3420. push bc
  3421. ld a, [hl] ; Atk IV
  3422. swap a
  3423. and $1
  3424. sla a
  3425. sla a
  3426. sla a
  3427. ld b, a
  3428. ld a, [hli] ; Def IV
  3429. and $1
  3430. sla a
  3431. sla a
  3432. add b
  3433. ld b, a
  3434. ld a, [hl] ; Spd IV
  3435. swap a
  3436. and $1
  3437. sla a
  3438. add b
  3439. ld b, a
  3440. ld a, [hl] ; Spc IV
  3441. and $1
  3442. add b ; HP IV: LSB of the other 4 IVs
  3443. pop bc
  3444. jr .calcStatFromIV
  3445. .getAttackIV
  3446. ld a, [hl]
  3447. swap a
  3448. and $f
  3449. jr .calcStatFromIV
  3450. .getDefenseIV
  3451. ld a, [hl]
  3452. and $f
  3453. jr .calcStatFromIV
  3454. .getSpeedIV
  3455. inc hl
  3456. ld a, [hl]
  3457. swap a
  3458. and $f
  3459. jr .calcStatFromIV
  3460. .getSpecialIV
  3461. inc hl
  3462. ld a, [hl]
  3463. and $f
  3464. .calcStatFromIV
  3465. ld d, $0
  3466. add e
  3467. ld e, a
  3468. jr nc, .noCarry
  3469. inc d ; de = Base + IV
  3470. .noCarry
  3471. sla e
  3472. rl d ; de = (Base + IV) * 2
  3473. srl b
  3474. srl b ; b = ceil(Sqrt(stat exp)) / 4
  3475. ld a, b
  3476. add e
  3477. jr nc, .noCarry2
  3478. inc d ; de = (Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4
  3479. .noCarry2
  3480. ld [H_MULTIPLICAND+2], a
  3481. ld a, d
  3482. ld [H_MULTIPLICAND+1], a
  3483. xor a
  3484. ld [H_MULTIPLICAND], a
  3485. ld a, [wCurEnemyLVL]
  3486. ld [H_MULTIPLIER], a
  3487. call Multiply ; ((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level
  3488. ld a, [H_MULTIPLICAND]
  3489. ld [H_DIVIDEND], a
  3490. ld a, [H_MULTIPLICAND+1]
  3491. ld [H_DIVIDEND+1], a
  3492. ld a, [H_MULTIPLICAND+2]
  3493. ld [H_DIVIDEND+2], a
  3494. ld a, $64
  3495. ld [H_DIVISOR], a
  3496. ld a, $3
  3497. ld b, a
  3498. call Divide ; (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100
  3499. ld a, c
  3500. cp $1
  3501. ld a, 5 ; + 5 for non-HP stat
  3502. jr nz, .notHPStat
  3503. ld a, [wCurEnemyLVL]
  3504. ld b, a
  3505. ld a, [H_MULTIPLICAND+2]
  3506. add b
  3507. ld [H_MULTIPLICAND+2], a
  3508. jr nc, .noCarry3
  3509. ld a, [H_MULTIPLICAND+1]
  3510. inc a
  3511. ld [H_MULTIPLICAND+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level
  3512. .noCarry3
  3513. ld a, 10 ; +10 for HP stat
  3514. .notHPStat
  3515. ld b, a
  3516. ld a, [H_MULTIPLICAND+2]
  3517. add b
  3518. ld [H_MULTIPLICAND+2], a
  3519. jr nc, .noCarry4
  3520. ld a, [H_MULTIPLICAND+1]
  3521. inc a ; non-HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + 5
  3522. ld [H_MULTIPLICAND+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level + 10
  3523. .noCarry4
  3524. ld a, [H_MULTIPLICAND+1] ; check for overflow (>999)
  3525. cp 999 / $100 + 1
  3526. jr nc, .overflow
  3527. cp 999 / $100
  3528. jr c, .noOverflow
  3529. ld a, [H_MULTIPLICAND+2]
  3530. cp 999 % $100 + 1
  3531. jr c, .noOverflow
  3532. .overflow
  3533. ld a, 999 / $100 ; overflow: cap at 999
  3534. ld [H_MULTIPLICAND+1], a
  3535. ld a, 999 % $100
  3536. ld [H_MULTIPLICAND+2], a
  3537. .noOverflow
  3538. pop bc
  3539. pop de
  3540. pop hl
  3541. ret
  3542. AddEnemyMonToPlayerParty::
  3543. ld a, [H_LOADEDROMBANK]
  3544. push af
  3545. ld a, BANK(_AddEnemyMonToPlayerParty)
  3546. ld [H_LOADEDROMBANK], a
  3547. ld [MBC1RomBank], a
  3548. call _AddEnemyMonToPlayerParty
  3549. pop bc
  3550. ld a, b
  3551. ld [H_LOADEDROMBANK], a
  3552. ld [MBC1RomBank], a
  3553. ret
  3554. MoveMon::
  3555. ld a, [H_LOADEDROMBANK]
  3556. push af
  3557. ld a, BANK(_MoveMon)
  3558. ld [H_LOADEDROMBANK], a
  3559. ld [MBC1RomBank], a
  3560. call _MoveMon
  3561. pop bc
  3562. ld a, b
  3563. ld [H_LOADEDROMBANK], a
  3564. ld [MBC1RomBank], a
  3565. ret
  3566. ; skips a text entries, each of size NAME_LENGTH (like trainer name, OT name, rival name, ...)
  3567. ; hl: base pointer, will be incremented by NAME_LENGTH * a
  3568. SkipFixedLengthTextEntries::
  3569. and a
  3570. ret z
  3571. ld bc, NAME_LENGTH
  3572. .skipLoop
  3573. add hl, bc
  3574. dec a
  3575. jr nz, .skipLoop
  3576. ret
  3577. AddNTimes::
  3578. ; add bc to hl a times
  3579. and a
  3580. ret z
  3581. .loop
  3582. add hl, bc
  3583. dec a
  3584. jr nz, .loop
  3585. ret
  3586. ; Compare strings, c bytes in length, at de and hl.
  3587. ; Often used to compare big endian numbers in battle calculations.
  3588. StringCmp::
  3589. ld a, [de]
  3590. cp [hl]
  3591. ret nz
  3592. inc de
  3593. inc hl
  3594. dec c
  3595. jr nz, StringCmp
  3596. ret
  3597. ; INPUT:
  3598. ; a = oam block index (each block is 4 oam entries)
  3599. ; b = Y coordinate of upper left corner of sprite
  3600. ; c = X coordinate of upper left corner of sprite
  3601. ; de = base address of 4 tile number and attribute pairs
  3602. WriteOAMBlock::
  3603. ld h, wOAMBuffer / $100
  3604. swap a ; multiply by 16
  3605. ld l, a
  3606. call .writeOneEntry ; upper left
  3607. push bc
  3608. ld a, 8
  3609. add c
  3610. ld c, a
  3611. call .writeOneEntry ; upper right
  3612. pop bc
  3613. ld a, 8
  3614. add b
  3615. ld b, a
  3616. call .writeOneEntry ; lower left
  3617. ld a, 8
  3618. add c
  3619. ld c, a
  3620. ; lower right
  3621. .writeOneEntry
  3622. ld [hl], b ; Y coordinate
  3623. inc hl
  3624. ld [hl], c ; X coordinate
  3625. inc hl
  3626. ld a, [de] ; tile number
  3627. inc de
  3628. ld [hli], a
  3629. ld a, [de] ; attribute
  3630. inc de
  3631. ld [hli], a
  3632. ret
  3633. HandleMenuInput::
  3634. xor a
  3635. ld [wPartyMenuAnimMonEnabled], a
  3636. HandleMenuInput_::
  3637. ld a, [H_DOWNARROWBLINKCNT1]
  3638. push af
  3639. ld a, [H_DOWNARROWBLINKCNT2]
  3640. push af ; save existing values on stack
  3641. xor a
  3642. ld [H_DOWNARROWBLINKCNT1], a ; blinking down arrow timing value 1
  3643. ld a, 6
  3644. ld [H_DOWNARROWBLINKCNT2], a ; blinking down arrow timing value 2
  3645. .loop1
  3646. xor a
  3647. ld [wAnimCounter], a ; counter for pokemon shaking animation
  3648. call PlaceMenuCursor
  3649. call Delay3
  3650. .loop2
  3651. push hl
  3652. ld a, [wPartyMenuAnimMonEnabled]
  3653. and a ; is it a pokemon selection menu?
  3654. jr z, .getJoypadState
  3655. callba AnimatePartyMon ; shake mini sprite of selected pokemon
  3656. .getJoypadState
  3657. pop hl
  3658. call JoypadLowSensitivity
  3659. ld a, [hJoy5]
  3660. and a ; was a key pressed?
  3661. jr nz, .keyPressed
  3662. push hl
  3663. coord hl, 18, 11 ; coordinates of blinking down arrow in some menus
  3664. call HandleDownArrowBlinkTiming ; blink down arrow (if any)
  3665. pop hl
  3666. ld a, [wMenuJoypadPollCount]
  3667. dec a
  3668. jr z, .giveUpWaiting
  3669. jr .loop2
  3670. .giveUpWaiting
  3671. ; if a key wasn't pressed within the specified number of checks
  3672. pop af
  3673. ld [H_DOWNARROWBLINKCNT2], a
  3674. pop af
  3675. ld [H_DOWNARROWBLINKCNT1], a ; restore previous values
  3676. xor a
  3677. ld [wMenuWrappingEnabled], a ; disable menu wrapping
  3678. ret
  3679. .keyPressed
  3680. xor a
  3681. ld [wCheckFor180DegreeTurn], a
  3682. ld a, [hJoy5]
  3683. ld b, a
  3684. bit 6, a ; pressed Up key?
  3685. jr z, .checkIfDownPressed
  3686. .upPressed
  3687. ld a, [wCurrentMenuItem] ; selected menu item
  3688. and a ; already at the top of the menu?
  3689. jr z, .alreadyAtTop
  3690. .notAtTop
  3691. dec a
  3692. ld [wCurrentMenuItem], a ; move selected menu item up one space
  3693. jr .checkOtherKeys
  3694. .alreadyAtTop
  3695. ld a, [wMenuWrappingEnabled]
  3696. and a ; is wrapping around enabled?
  3697. jr z, .noWrappingAround
  3698. ld a, [wMaxMenuItem]
  3699. ld [wCurrentMenuItem], a ; wrap to the bottom of the menu
  3700. jr .checkOtherKeys
  3701. .checkIfDownPressed
  3702. bit 7, a
  3703. jr z, .checkOtherKeys
  3704. .downPressed
  3705. ld a, [wCurrentMenuItem]
  3706. inc a
  3707. ld c, a
  3708. ld a, [wMaxMenuItem]
  3709. cp c
  3710. jr nc, .notAtBottom
  3711. .alreadyAtBottom
  3712. ld a, [wMenuWrappingEnabled]
  3713. and a ; is wrapping around enabled?
  3714. jr z, .noWrappingAround
  3715. ld c, $00 ; wrap from bottom to top
  3716. .notAtBottom
  3717. ld a, c
  3718. ld [wCurrentMenuItem], a
  3719. .checkOtherKeys
  3720. ld a, [wMenuWatchedKeys]
  3721. and b ; does the menu care about any of the pressed keys?
  3722. jp z, .loop1
  3723. .checkIfAButtonOrBButtonPressed
  3724. ld a, [hJoy5]
  3725. and A_BUTTON | B_BUTTON
  3726. jr z, .skipPlayingSound
  3727. .AButtonOrBButtonPressed
  3728. push hl
  3729. ld hl, wFlags_0xcd60
  3730. bit 5, [hl]
  3731. pop hl
  3732. jr nz, .skipPlayingSound
  3733. ld a, SFX_PRESS_AB
  3734. call PlaySound
  3735. .skipPlayingSound
  3736. pop af
  3737. ld [H_DOWNARROWBLINKCNT2], a
  3738. pop af
  3739. ld [H_DOWNARROWBLINKCNT1], a ; restore previous values
  3740. xor a
  3741. ld [wMenuWrappingEnabled], a ; disable menu wrapping
  3742. ld a, [hJoy5]
  3743. ret
  3744. .noWrappingAround
  3745. ld a, [wMenuWatchMovingOutOfBounds]
  3746. and a ; should we return if the user tried to go past the top or bottom?
  3747. jr z, .checkOtherKeys
  3748. jr .checkIfAButtonOrBButtonPressed
  3749. PlaceMenuCursor::
  3750. ld a, [wTopMenuItemY]
  3751. and a ; is the y coordinate 0?
  3752. jr z, .adjustForXCoord
  3753. coord hl, 0, 0
  3754. ld bc, SCREEN_WIDTH
  3755. .topMenuItemLoop
  3756. add hl, bc
  3757. dec a
  3758. jr nz, .topMenuItemLoop
  3759. .adjustForXCoord
  3760. ld a, [wTopMenuItemX]
  3761. ld b, 0
  3762. ld c, a
  3763. add hl, bc
  3764. push hl
  3765. ld a, [wLastMenuItem]
  3766. and a ; was the previous menu id 0?
  3767. jr z, .checkForArrow1
  3768. push af
  3769. ld a, [hFlags_0xFFF6]
  3770. bit 1, a ; is the menu double spaced?
  3771. jr z, .doubleSpaced1
  3772. ld bc, 20
  3773. jr .getOldMenuItemScreenPosition
  3774. .doubleSpaced1
  3775. ld bc, 40
  3776. .getOldMenuItemScreenPosition
  3777. pop af
  3778. .oldMenuItemLoop
  3779. add hl, bc
  3780. dec a
  3781. jr nz, .oldMenuItemLoop
  3782. .checkForArrow1
  3783. ld a, [hl]
  3784. cp "▶" ; was an arrow next to the previously selected menu item?
  3785. jr nz, .skipClearingArrow
  3786. .clearArrow
  3787. ld a, [wTileBehindCursor]
  3788. ld [hl], a
  3789. .skipClearingArrow
  3790. pop hl
  3791. ld a, [wCurrentMenuItem]
  3792. and a
  3793. jr z, .checkForArrow2
  3794. push af
  3795. ld a, [hFlags_0xFFF6]
  3796. bit 1, a ; is the menu double spaced?
  3797. jr z, .doubleSpaced2
  3798. ld bc, 20
  3799. jr .getCurrentMenuItemScreenPosition
  3800. .doubleSpaced2
  3801. ld bc, 40
  3802. .getCurrentMenuItemScreenPosition
  3803. pop af
  3804. .currentMenuItemLoop
  3805. add hl, bc
  3806. dec a
  3807. jr nz, .currentMenuItemLoop
  3808. .checkForArrow2
  3809. ld a, [hl]
  3810. cp "▶" ; has the right arrow already been placed?
  3811. jr z, .skipSavingTile ; if so, don't lose the saved tile
  3812. ld [wTileBehindCursor], a ; save tile before overwriting with right arrow
  3813. .skipSavingTile
  3814. ld a, "▶" ; place right arrow
  3815. ld [hl], a
  3816. ld a, l
  3817. ld [wMenuCursorLocation], a
  3818. ld a, h
  3819. ld [wMenuCursorLocation + 1], a
  3820. ld a, [wCurrentMenuItem]
  3821. ld [wLastMenuItem], a
  3822. ret
  3823. ; This is used to mark a menu cursor other than the one currently being
  3824. ; manipulated. In the case of submenus, this is used to show the location of
  3825. ; the menu cursor in the parent menu. In the case of swapping items in list,
  3826. ; this is used to mark the item that was first chosen to be swapped.
  3827. PlaceUnfilledArrowMenuCursor::
  3828. ld b, a
  3829. ld a, [wMenuCursorLocation]
  3830. ld l, a
  3831. ld a, [wMenuCursorLocation + 1]
  3832. ld h, a
  3833. ld [hl], $ec ; outline of right arrow
  3834. ld a, b
  3835. ret
  3836. ; Replaces the menu cursor with a blank space.
  3837. EraseMenuCursor::
  3838. ld a, [wMenuCursorLocation]
  3839. ld l, a
  3840. ld a, [wMenuCursorLocation + 1]
  3841. ld h, a
  3842. ld [hl], " "
  3843. ret
  3844. ; This toggles a blinking down arrow at hl on and off after a delay has passed.
  3845. ; This is often called even when no blinking is occurring.
  3846. ; The reason is that most functions that call this initialize H_DOWNARROWBLINKCNT1 to 0.
  3847. ; The effect is that if the tile at hl is initialized with a down arrow,
  3848. ; this function will toggle that down arrow on and off, but if the tile isn't
  3849. ; initialized with a down arrow, this function does nothing.
  3850. ; That allows this to be called without worrying about if a down arrow should
  3851. ; be blinking.
  3852. HandleDownArrowBlinkTiming::
  3853. ld a, [hl]
  3854. ld b, a
  3855. ld a, "▼"
  3856. cp b
  3857. jr nz, .downArrowOff
  3858. .downArrowOn
  3859. ld a, [H_DOWNARROWBLINKCNT1]
  3860. dec a
  3861. ld [H_DOWNARROWBLINKCNT1], a
  3862. ret nz
  3863. ld a, [H_DOWNARROWBLINKCNT2]
  3864. dec a
  3865. ld [H_DOWNARROWBLINKCNT2], a
  3866. ret nz
  3867. ld a, " "
  3868. ld [hl], a
  3869. ld a, $ff
  3870. ld [H_DOWNARROWBLINKCNT1], a
  3871. ld a, $06
  3872. ld [H_DOWNARROWBLINKCNT2], a
  3873. ret
  3874. .downArrowOff
  3875. ld a, [H_DOWNARROWBLINKCNT1]
  3876. and a
  3877. ret z
  3878. dec a
  3879. ld [H_DOWNARROWBLINKCNT1], a
  3880. ret nz
  3881. dec a
  3882. ld [H_DOWNARROWBLINKCNT1], a
  3883. ld a, [H_DOWNARROWBLINKCNT2]
  3884. dec a
  3885. ld [H_DOWNARROWBLINKCNT2], a
  3886. ret nz
  3887. ld a, $06
  3888. ld [H_DOWNARROWBLINKCNT2], a
  3889. ld a, "▼"
  3890. ld [hl], a
  3891. ret
  3892. ; The following code either enables or disables the automatic drawing of
  3893. ; text boxes by DisplayTextID. Both functions cause DisplayTextID to wait
  3894. ; for a button press after displaying text (unless [wEnteringCableClub] is set).
  3895. EnableAutoTextBoxDrawing::
  3896. xor a
  3897. jr AutoTextBoxDrawingCommon
  3898. DisableAutoTextBoxDrawing::
  3899. ld a, $01
  3900. AutoTextBoxDrawingCommon::
  3901. ld [wAutoTextBoxDrawingControl], a
  3902. xor a
  3903. ld [wDoNotWaitForButtonPressAfterDisplayingText], a ; make DisplayTextID wait for button press
  3904. ret
  3905. PrintText::
  3906. ; Print text hl at (1, 14).
  3907. push hl
  3908. ld a, MESSAGE_BOX
  3909. ld [wTextBoxID], a
  3910. call DisplayTextBoxID
  3911. call UpdateSprites
  3912. call Delay3
  3913. pop hl
  3914. PrintText_NoCreatingTextBox::
  3915. coord bc, 1, 14
  3916. jp TextCommandProcessor
  3917. PrintNumber::
  3918. ; Print the c-digit, b-byte value at de.
  3919. ; Allows 2 to 7 digits. For 1-digit numbers, add
  3920. ; the value to char "0" instead of calling PrintNumber.
  3921. ; Flags LEADING_ZEROES and LEFT_ALIGN can be given
  3922. ; in bits 7 and 6 of b respectively.
  3923. push bc
  3924. xor a
  3925. ld [H_PASTLEADINGZEROES], a
  3926. ld [H_NUMTOPRINT], a
  3927. ld [H_NUMTOPRINT + 1], a
  3928. ld a, b
  3929. and $f
  3930. cp 1
  3931. jr z, .byte
  3932. cp 2
  3933. jr z, .word
  3934. .long
  3935. ld a, [de]
  3936. ld [H_NUMTOPRINT], a
  3937. inc de
  3938. ld a, [de]
  3939. ld [H_NUMTOPRINT + 1], a
  3940. inc de
  3941. ld a, [de]
  3942. ld [H_NUMTOPRINT + 2], a
  3943. jr .start
  3944. .word
  3945. ld a, [de]
  3946. ld [H_NUMTOPRINT + 1], a
  3947. inc de
  3948. ld a, [de]
  3949. ld [H_NUMTOPRINT + 2], a
  3950. jr .start
  3951. .byte
  3952. ld a, [de]
  3953. ld [H_NUMTOPRINT + 2], a
  3954. .start
  3955. push de
  3956. ld d, b
  3957. ld a, c
  3958. ld b, a
  3959. xor a
  3960. ld c, a
  3961. ld a, b
  3962. cp 2
  3963. jr z, .tens
  3964. cp 3
  3965. jr z, .hundreds
  3966. cp 4
  3967. jr z, .thousands
  3968. cp 5
  3969. jr z, .ten_thousands
  3970. cp 6
  3971. jr z, .hundred_thousands
  3972. print_digit: macro
  3973. if (\1) / $10000
  3974. ld a, \1 / $10000 % $100
  3975. else xor a
  3976. endc
  3977. ld [H_POWEROFTEN + 0], a
  3978. if (\1) / $100
  3979. ld a, \1 / $100 % $100
  3980. else xor a
  3981. endc
  3982. ld [H_POWEROFTEN + 1], a
  3983. ld a, \1 / $1 % $100
  3984. ld [H_POWEROFTEN + 2], a
  3985. call .PrintDigit
  3986. call .NextDigit
  3987. endm
  3988. .millions print_digit 1000000
  3989. .hundred_thousands print_digit 100000
  3990. .ten_thousands print_digit 10000
  3991. .thousands print_digit 1000
  3992. .hundreds print_digit 100
  3993. .tens
  3994. ld c, 0
  3995. ld a, [H_NUMTOPRINT + 2]
  3996. .mod
  3997. cp 10
  3998. jr c, .ok
  3999. sub 10
  4000. inc c
  4001. jr .mod
  4002. .ok
  4003. ld b, a
  4004. ld a, [H_PASTLEADINGZEROES]
  4005. or c
  4006. ld [H_PASTLEADINGZEROES], a
  4007. jr nz, .past
  4008. call .PrintLeadingZero
  4009. jr .next
  4010. .past
  4011. ld a, "0"
  4012. add c
  4013. ld [hl], a
  4014. .next
  4015. call .NextDigit
  4016. .ones
  4017. ld a, "0"
  4018. add b
  4019. ld [hli], a
  4020. pop de
  4021. dec de
  4022. pop bc
  4023. ret
  4024. .PrintDigit:
  4025. ; Divide by the current decimal place.
  4026. ; Print the quotient, and keep the modulus.
  4027. ld c, 0
  4028. .loop
  4029. ld a, [H_POWEROFTEN]
  4030. ld b, a
  4031. ld a, [H_NUMTOPRINT]
  4032. ld [H_SAVEDNUMTOPRINT], a
  4033. cp b
  4034. jr c, .underflow0
  4035. sub b
  4036. ld [H_NUMTOPRINT], a
  4037. ld a, [H_POWEROFTEN + 1]
  4038. ld b, a
  4039. ld a, [H_NUMTOPRINT + 1]
  4040. ld [H_SAVEDNUMTOPRINT + 1], a
  4041. cp b
  4042. jr nc, .noborrow1
  4043. ld a, [H_NUMTOPRINT]
  4044. or 0
  4045. jr z, .underflow1
  4046. dec a
  4047. ld [H_NUMTOPRINT], a
  4048. ld a, [H_NUMTOPRINT + 1]
  4049. .noborrow1
  4050. sub b
  4051. ld [H_NUMTOPRINT + 1], a
  4052. ld a, [H_POWEROFTEN + 2]
  4053. ld b, a
  4054. ld a, [H_NUMTOPRINT + 2]
  4055. ld [H_SAVEDNUMTOPRINT + 2], a
  4056. cp b
  4057. jr nc, .noborrow2
  4058. ld a, [H_NUMTOPRINT + 1]
  4059. and a
  4060. jr nz, .borrowed
  4061. ld a, [H_NUMTOPRINT]
  4062. and a
  4063. jr z, .underflow2
  4064. dec a
  4065. ld [H_NUMTOPRINT], a
  4066. xor a
  4067. .borrowed
  4068. dec a
  4069. ld [H_NUMTOPRINT + 1], a
  4070. ld a, [H_NUMTOPRINT + 2]
  4071. .noborrow2
  4072. sub b
  4073. ld [H_NUMTOPRINT + 2], a
  4074. inc c
  4075. jr .loop
  4076. .underflow2
  4077. ld a, [H_SAVEDNUMTOPRINT + 1]
  4078. ld [H_NUMTOPRINT + 1], a
  4079. .underflow1
  4080. ld a, [H_SAVEDNUMTOPRINT]
  4081. ld [H_NUMTOPRINT], a
  4082. .underflow0
  4083. ld a, [H_PASTLEADINGZEROES]
  4084. or c
  4085. jr z, .PrintLeadingZero
  4086. ld a, "0"
  4087. add c
  4088. ld [hl], a
  4089. ld [H_PASTLEADINGZEROES], a
  4090. ret
  4091. .PrintLeadingZero:
  4092. bit BIT_LEADING_ZEROES, d
  4093. ret z
  4094. ld [hl], "0"
  4095. ret
  4096. .NextDigit:
  4097. ; Increment unless the number is left-aligned,
  4098. ; leading zeroes are not printed, and no digits
  4099. ; have been printed yet.
  4100. bit BIT_LEADING_ZEROES, d
  4101. jr nz, .inc
  4102. bit BIT_LEFT_ALIGN, d
  4103. jr z, .inc
  4104. ld a, [H_PASTLEADINGZEROES]
  4105. and a
  4106. ret z
  4107. .inc
  4108. inc hl
  4109. ret
  4110. CallFunctionInTable::
  4111. ; Call function a in jumptable hl.
  4112. ; de is not preserved.
  4113. push hl
  4114. push de
  4115. push bc
  4116. add a
  4117. ld d, 0
  4118. ld e, a
  4119. add hl, de
  4120. ld a, [hli]
  4121. ld h, [hl]
  4122. ld l, a
  4123. ld de, .returnAddress
  4124. push de
  4125. jp hl
  4126. .returnAddress
  4127. pop bc
  4128. pop de
  4129. pop hl
  4130. ret
  4131. IsInArray::
  4132. ; Search an array at hl for the value in a.
  4133. ; Entry size is de bytes.
  4134. ; Return count b and carry if found.
  4135. ld b, 0
  4136. IsInRestOfArray::
  4137. ld c, a
  4138. .loop
  4139. ld a, [hl]
  4140. cp -1
  4141. jr z, .notfound
  4142. cp c
  4143. jr z, .found
  4144. inc b
  4145. add hl, de
  4146. jr .loop
  4147. .notfound
  4148. and a
  4149. ret
  4150. .found
  4151. scf
  4152. ret
  4153. RestoreScreenTilesAndReloadTilePatterns::
  4154. call ClearSprites
  4155. ld a, $1
  4156. ld [wUpdateSpritesEnabled], a
  4157. call ReloadMapSpriteTilePatterns
  4158. call LoadScreenTilesFromBuffer2
  4159. call LoadTextBoxTilePatterns
  4160. call RunDefaultPaletteCommand
  4161. jr Delay3
  4162. GBPalWhiteOutWithDelay3::
  4163. call GBPalWhiteOut
  4164. Delay3::
  4165. ; The bg map is updated each frame in thirds.
  4166. ; Wait three frames to let the bg map fully update.
  4167. ld c, 3
  4168. jp DelayFrames
  4169. GBPalNormal::
  4170. ; Reset BGP and OBP0.
  4171. ld a, %11100100 ; 3210
  4172. ld [rBGP], a
  4173. ld a, %11010000 ; 3100
  4174. ld [rOBP0], a
  4175. ret
  4176. GBPalWhiteOut::
  4177. ; White out all palettes.
  4178. xor a
  4179. ld [rBGP], a
  4180. ld [rOBP0], a
  4181. ld [rOBP1], a
  4182. ret
  4183. RunDefaultPaletteCommand::
  4184. ld b, $ff
  4185. RunPaletteCommand::
  4186. ld a, [wOnSGB]
  4187. and a
  4188. ret z
  4189. predef_jump _RunPaletteCommand
  4190. GetHealthBarColor::
  4191. ; Return at hl the palette of
  4192. ; an HP bar e pixels long.
  4193. ld a, e
  4194. cp 27
  4195. ld d, 0 ; green
  4196. jr nc, .gotColor
  4197. cp 10
  4198. inc d ; yellow
  4199. jr nc, .gotColor
  4200. inc d ; red
  4201. .gotColor
  4202. ld [hl], d
  4203. ret
  4204. ; Copy the current map's sprites' tile patterns to VRAM again after they have
  4205. ; been overwritten by other tile patterns.
  4206. ReloadMapSpriteTilePatterns::
  4207. ld hl, wFontLoaded
  4208. ld a, [hl]
  4209. push af
  4210. res 0, [hl]
  4211. push hl
  4212. xor a
  4213. ld [wSpriteSetID], a
  4214. call DisableLCD
  4215. callba InitMapSprites
  4216. call EnableLCD
  4217. pop hl
  4218. pop af
  4219. ld [hl], a
  4220. call LoadPlayerSpriteGraphics
  4221. call LoadFontTilePatterns
  4222. jp UpdateSprites
  4223. GiveItem::
  4224. ; Give player quantity c of item b,
  4225. ; and copy the item's name to wcf4b.
  4226. ; Return carry on success.
  4227. ld a, b
  4228. ld [wd11e], a
  4229. ld [wcf91], a
  4230. ld a, c
  4231. ld [wItemQuantity], a
  4232. ld hl, wNumBagItems
  4233. call AddItemToInventory
  4234. ret nc
  4235. call GetItemName
  4236. call CopyStringToCF4B
  4237. scf
  4238. ret
  4239. GivePokemon::
  4240. ; Give the player monster b at level c.
  4241. ld a, b
  4242. ld [wcf91], a
  4243. ld a, c
  4244. ld [wCurEnemyLVL], a
  4245. xor a ; PLAYER_PARTY_DATA
  4246. ld [wMonDataLocation], a
  4247. jpba _GivePokemon
  4248. Random::
  4249. ; Return a random number in a.
  4250. ; For battles, use BattleRandom.
  4251. push hl
  4252. push de
  4253. push bc
  4254. callba Random_
  4255. ld a, [hRandomAdd]
  4256. pop bc
  4257. pop de
  4258. pop hl
  4259. ret
  4260. INCLUDE "home/predef.asm"
  4261. UpdateCinnabarGymGateTileBlocks::
  4262. jpba UpdateCinnabarGymGateTileBlocks_
  4263. CheckForHiddenObjectOrBookshelfOrCardKeyDoor::
  4264. ld a, [H_LOADEDROMBANK]
  4265. push af
  4266. ld a, [hJoyHeld]
  4267. bit 0, a ; A button
  4268. jr z, .nothingFound
  4269. ; A button is pressed
  4270. ld a, Bank(CheckForHiddenObject)
  4271. ld [MBC1RomBank], a
  4272. ld [H_LOADEDROMBANK], a
  4273. call CheckForHiddenObject
  4274. ld a, [$ffee]
  4275. and a
  4276. jr nz, .hiddenObjectNotFound
  4277. ld a, [wHiddenObjectFunctionRomBank]
  4278. ld [MBC1RomBank], a
  4279. ld [H_LOADEDROMBANK], a
  4280. ld de, .returnAddress
  4281. push de
  4282. jp hl
  4283. .returnAddress
  4284. xor a
  4285. jr .done
  4286. .hiddenObjectNotFound
  4287. callba PrintBookshelfText
  4288. ld a, [$ffdb]
  4289. and a
  4290. jr z, .done
  4291. .nothingFound
  4292. ld a, $ff
  4293. .done
  4294. ld [$ffeb], a
  4295. pop af
  4296. ld [MBC1RomBank], a
  4297. ld [H_LOADEDROMBANK], a
  4298. ret
  4299. PrintPredefTextID::
  4300. ld [hSpriteIndexOrTextID], a
  4301. ld hl, TextPredefs
  4302. call SetMapTextPointer
  4303. ld hl, wTextPredefFlag
  4304. set 0, [hl]
  4305. call DisplayTextID
  4306. RestoreMapTextPointer::
  4307. ld hl, wMapTextPtr
  4308. ld a, [$ffec]
  4309. ld [hli], a
  4310. ld a, [$ffec + 1]
  4311. ld [hl], a
  4312. ret
  4313. SetMapTextPointer::
  4314. ld a, [wMapTextPtr]
  4315. ld [$ffec], a
  4316. ld a, [wMapTextPtr + 1]
  4317. ld [$ffec + 1], a
  4318. ld a, l
  4319. ld [wMapTextPtr], a
  4320. ld a, h
  4321. ld [wMapTextPtr + 1], a
  4322. ret
  4323. TextPredefs::
  4324. const_value = 1
  4325. add_tx_pre CardKeySuccessText ; 01
  4326. add_tx_pre CardKeyFailText ; 02
  4327. add_tx_pre RedBedroomPCText ; 03
  4328. add_tx_pre RedBedroomSNESText ; 04
  4329. add_tx_pre PushStartText ; 05
  4330. add_tx_pre SaveOptionText ; 06
  4331. add_tx_pre StrengthsAndWeaknessesText ; 07
  4332. add_tx_pre OakLabEmailText ; 08
  4333. add_tx_pre AerodactylFossilText ; 09
  4334. add_tx_pre Route15UpstairsBinocularsText ; 0A
  4335. add_tx_pre KabutopsFossilText ; 0B
  4336. add_tx_pre GymStatueText1 ; 0C
  4337. add_tx_pre GymStatueText2 ; 0D
  4338. add_tx_pre BookcaseText ; 0E
  4339. add_tx_pre ViridianCityPokecenterBenchGuyText ; 0F
  4340. add_tx_pre PewterCityPokecenterBenchGuyText ; 10
  4341. add_tx_pre CeruleanCityPokecenterBenchGuyText ; 11
  4342. add_tx_pre LavenderCityPokecenterBenchGuyText ; 12
  4343. add_tx_pre VermilionCityPokecenterBenchGuyText ; 13
  4344. add_tx_pre CeladonCityPokecenterBenchGuyText ; 14
  4345. add_tx_pre CeladonCityHotelText ; 15
  4346. add_tx_pre FuchsiaCityPokecenterBenchGuyText ; 16
  4347. add_tx_pre CinnabarIslandPokecenterBenchGuyText ; 17
  4348. add_tx_pre SaffronCityPokecenterBenchGuyText ; 18
  4349. add_tx_pre MtMoonPokecenterBenchGuyText ; 19
  4350. add_tx_pre RockTunnelPokecenterBenchGuyText ; 1A
  4351. add_tx_pre UnusedBenchGuyText1 ; 1B XXX unused
  4352. add_tx_pre UnusedBenchGuyText2 ; 1C XXX unused
  4353. add_tx_pre UnusedBenchGuyText3 ; 1D XXX unused
  4354. add_tx_pre UnusedPredefText ; 1E XXX unused
  4355. add_tx_pre PokemonCenterPCText ; 1F
  4356. add_tx_pre ViridianSchoolNotebook ; 20
  4357. add_tx_pre ViridianSchoolBlackboard ; 21
  4358. add_tx_pre JustAMomentText ; 22
  4359. add_tx_pre OpenBillsPCText ; 23
  4360. add_tx_pre FoundHiddenItemText ; 24
  4361. add_tx_pre HiddenItemBagFullText ; 25 XXX unused
  4362. add_tx_pre VermilionGymTrashText ; 26
  4363. add_tx_pre IndigoPlateauHQText ; 27
  4364. add_tx_pre GameCornerOutOfOrderText ; 28
  4365. add_tx_pre GameCornerOutToLunchText ; 29
  4366. add_tx_pre GameCornerSomeonesKeysText ; 2A
  4367. add_tx_pre FoundHiddenCoinsText ; 2B
  4368. add_tx_pre DroppedHiddenCoinsText ; 2C
  4369. add_tx_pre BillsHouseMonitorText ; 2D
  4370. add_tx_pre BillsHouseInitiatedText ; 2E
  4371. add_tx_pre BillsHousePokemonList ; 2F
  4372. add_tx_pre MagazinesText ; 30
  4373. add_tx_pre CinnabarGymQuiz ; 31
  4374. add_tx_pre GameCornerNoCoinsText ; 32
  4375. add_tx_pre GameCornerCoinCaseText ; 33
  4376. add_tx_pre LinkCableHelp ; 34
  4377. add_tx_pre TMNotebook ; 35
  4378. add_tx_pre FightingDojoText ; 36
  4379. add_tx_pre EnemiesOnEverySideText ; 37
  4380. add_tx_pre WhatGoesAroundComesAroundText ; 38
  4381. add_tx_pre NewBicycleText ; 39
  4382. add_tx_pre IndigoPlateauStatues ; 3A
  4383. add_tx_pre VermilionGymTrashSuccessText1 ; 3B
  4384. add_tx_pre VermilionGymTrashSuccessText2 ; 3C XXX unused
  4385. add_tx_pre VermilionGymTrashSuccessText3 ; 3D
  4386. add_tx_pre VermilionGymTrashFailText ; 3E
  4387. add_tx_pre TownMapText ; 3F
  4388. add_tx_pre BookOrSculptureText ; 40
  4389. add_tx_pre ElevatorText ; 41
  4390. add_tx_pre PokemonStuffText ; 42