core.asm 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720
  1. BattleCore:
  2. ; These are move effects (second value from the Moves table in bank $E).
  3. ResidualEffects1:
  4. ; most non-side effects
  5. db CONVERSION_EFFECT
  6. db HAZE_EFFECT
  7. db SWITCH_AND_TELEPORT_EFFECT
  8. db MIST_EFFECT
  9. db FOCUS_ENERGY_EFFECT
  10. db CONFUSION_EFFECT
  11. db HEAL_EFFECT
  12. db TRANSFORM_EFFECT
  13. db LIGHT_SCREEN_EFFECT
  14. db REFLECT_EFFECT
  15. db POISON_EFFECT
  16. db PARALYZE_EFFECT
  17. db SUBSTITUTE_EFFECT
  18. db MIMIC_EFFECT
  19. db LEECH_SEED_EFFECT
  20. db SPLASH_EFFECT
  21. db -1
  22. SetDamageEffects:
  23. ; moves that do damage but not through normal calculations
  24. ; e.g., Super Fang, Psywave
  25. db SUPER_FANG_EFFECT
  26. db SPECIAL_DAMAGE_EFFECT
  27. db -1
  28. ResidualEffects2:
  29. ; non-side effects not included in ResidualEffects1
  30. ; stat-affecting moves, sleep-inflicting moves, and Bide
  31. ; e.g., Meditate, Bide, Hypnosis
  32. db $01
  33. db ATTACK_UP1_EFFECT
  34. db DEFENSE_UP1_EFFECT
  35. db SPEED_UP1_EFFECT
  36. db SPECIAL_UP1_EFFECT
  37. db ACCURACY_UP1_EFFECT
  38. db EVASION_UP1_EFFECT
  39. db ATTACK_DOWN1_EFFECT
  40. db DEFENSE_DOWN1_EFFECT
  41. db SPEED_DOWN1_EFFECT
  42. db SPECIAL_DOWN1_EFFECT
  43. db ACCURACY_DOWN1_EFFECT
  44. db EVASION_DOWN1_EFFECT
  45. db BIDE_EFFECT
  46. db SLEEP_EFFECT
  47. db ATTACK_UP2_EFFECT
  48. db DEFENSE_UP2_EFFECT
  49. db SPEED_UP2_EFFECT
  50. db SPECIAL_UP2_EFFECT
  51. db ACCURACY_UP2_EFFECT
  52. db EVASION_UP2_EFFECT
  53. db ATTACK_DOWN2_EFFECT
  54. db DEFENSE_DOWN2_EFFECT
  55. db SPEED_DOWN2_EFFECT
  56. db SPECIAL_DOWN2_EFFECT
  57. db ACCURACY_DOWN2_EFFECT
  58. db EVASION_DOWN2_EFFECT
  59. db -1
  60. AlwaysHappenSideEffects:
  61. ; Attacks that aren't finished after they faint the opponent.
  62. db DRAIN_HP_EFFECT
  63. db EXPLODE_EFFECT
  64. db DREAM_EATER_EFFECT
  65. db PAY_DAY_EFFECT
  66. db TWO_TO_FIVE_ATTACKS_EFFECT
  67. db $1E
  68. db ATTACK_TWICE_EFFECT
  69. db RECOIL_EFFECT
  70. db TWINEEDLE_EFFECT
  71. db RAGE_EFFECT
  72. db -1
  73. SpecialEffects:
  74. ; Effects from arrays 2, 4, and 5B, minus Twineedle and Rage.
  75. ; Includes all effects that do not need to be called at the end of
  76. ; ExecutePlayerMove (or ExecuteEnemyMove), because they have already been handled
  77. db DRAIN_HP_EFFECT
  78. db EXPLODE_EFFECT
  79. db DREAM_EATER_EFFECT
  80. db PAY_DAY_EFFECT
  81. db SWIFT_EFFECT
  82. db TWO_TO_FIVE_ATTACKS_EFFECT
  83. db $1E
  84. db CHARGE_EFFECT
  85. db SUPER_FANG_EFFECT
  86. db SPECIAL_DAMAGE_EFFECT
  87. db FLY_EFFECT
  88. db ATTACK_TWICE_EFFECT
  89. db JUMP_KICK_EFFECT
  90. db RECOIL_EFFECT
  91. ; fallthrough to Next EffectsArray
  92. SpecialEffectsCont:
  93. ; damaging moves whose effect is executed prior to damage calculation
  94. db THRASH_PETAL_DANCE_EFFECT
  95. db TRAPPING_EFFECT
  96. db -1
  97. SlidePlayerAndEnemySilhouettesOnScreen:
  98. call LoadPlayerBackPic
  99. ld a, MESSAGE_BOX ; the usual text box at the bottom of the screen
  100. ld [wTextBoxID], a
  101. call DisplayTextBoxID
  102. coord hl, 1, 5
  103. lb bc, 3, 7
  104. call ClearScreenArea
  105. call DisableLCD
  106. call LoadFontTilePatterns
  107. call LoadHudAndHpBarAndStatusTilePatterns
  108. ld hl, vBGMap0
  109. ld bc, $400
  110. .clearBackgroundLoop
  111. ld a, " "
  112. ld [hli], a
  113. dec bc
  114. ld a, b
  115. or c
  116. jr nz, .clearBackgroundLoop
  117. ; copy the work RAM tile map to VRAM
  118. coord hl, 0, 0
  119. ld de, vBGMap0
  120. ld b, 18 ; number of rows
  121. .copyRowLoop
  122. ld c, 20 ; number of columns
  123. .copyColumnLoop
  124. ld a, [hli]
  125. ld [de], a
  126. inc e
  127. dec c
  128. jr nz, .copyColumnLoop
  129. ld a, 12 ; number of off screen tiles to the right of screen in VRAM
  130. add e ; skip the off screen tiles
  131. ld e, a
  132. jr nc, .noCarry
  133. inc d
  134. .noCarry
  135. dec b
  136. jr nz, .copyRowLoop
  137. call EnableLCD
  138. ld a, $90
  139. ld [hWY], a
  140. ld [rWY], a
  141. xor a
  142. ld [hTilesetType], a
  143. ld [hSCY], a
  144. dec a
  145. ld [wUpdateSpritesEnabled], a
  146. call Delay3
  147. xor a
  148. ld [H_AUTOBGTRANSFERENABLED], a
  149. ld b, $70
  150. ld c, $90
  151. ld a, c
  152. ld [hSCX], a
  153. call DelayFrame
  154. ld a, %11100100 ; inverted palette for silhouette effect
  155. ld [rBGP], a
  156. ld [rOBP0], a
  157. ld [rOBP1], a
  158. .slideSilhouettesLoop ; slide silhouettes of the player's pic and the enemy's pic onto the screen
  159. ld h, b
  160. ld l, $40
  161. call SetScrollXForSlidingPlayerBodyLeft ; begin background scrolling on line $40
  162. inc b
  163. inc b
  164. ld h, $0
  165. ld l, $60
  166. call SetScrollXForSlidingPlayerBodyLeft ; end background scrolling on line $60
  167. call SlidePlayerHeadLeft
  168. ld a, c
  169. ld [hSCX], a
  170. dec c
  171. dec c
  172. jr nz, .slideSilhouettesLoop
  173. ld a, $1
  174. ld [H_AUTOBGTRANSFERENABLED], a
  175. ld a, $31
  176. ld [hStartTileID], a
  177. coord hl, 1, 5
  178. predef CopyUncompressedPicToTilemap
  179. xor a
  180. ld [hWY], a
  181. ld [rWY], a
  182. inc a
  183. ld [H_AUTOBGTRANSFERENABLED], a
  184. call Delay3
  185. ld b, SET_PAL_BATTLE
  186. call RunPaletteCommand
  187. call HideSprites
  188. jpab PrintBeginningBattleText
  189. ; when a battle is starting, silhouettes of the player's pic and the enemy's pic are slid onto the screen
  190. ; the lower of the player's pic (his body) is part of the background, but his head is a sprite
  191. ; the reason for this is that it shares Y coordinates with the lower part of the enemy pic, so background scrolling wouldn't work for both pics
  192. ; instead, the enemy pic is part of the background and uses the scroll register, while the player's head is a sprite and is slid by changing its X coordinates in a loop
  193. SlidePlayerHeadLeft:
  194. push bc
  195. ld hl, wOAMBuffer + $01
  196. ld c, $15 ; number of OAM entries
  197. ld de, $4 ; size of OAM entry
  198. .loop
  199. dec [hl] ; decrement X
  200. dec [hl] ; decrement X
  201. add hl, de ; next OAM entry
  202. dec c
  203. jr nz, .loop
  204. pop bc
  205. ret
  206. SetScrollXForSlidingPlayerBodyLeft:
  207. ld a, [rLY]
  208. cp l
  209. jr nz, SetScrollXForSlidingPlayerBodyLeft
  210. ld a, h
  211. ld [rSCX], a
  212. .loop
  213. ld a, [rLY]
  214. cp h
  215. jr z, .loop
  216. ret
  217. StartBattle:
  218. xor a
  219. ld [wPartyGainExpFlags], a
  220. ld [wPartyFoughtCurrentEnemyFlags], a
  221. ld [wActionResultOrTookBattleTurn], a
  222. inc a
  223. ld [wFirstMonsNotOutYet], a
  224. ld hl, wEnemyMon1HP
  225. ld bc, wEnemyMon2 - wEnemyMon1 - 1
  226. ld d, $3
  227. .findFirstAliveEnemyMonLoop
  228. inc d
  229. ld a, [hli]
  230. or [hl]
  231. jr nz, .foundFirstAliveEnemyMon
  232. add hl, bc
  233. jr .findFirstAliveEnemyMonLoop
  234. .foundFirstAliveEnemyMon
  235. ld a, d
  236. ld [wSerialExchangeNybbleReceiveData], a
  237. ld a, [wIsInBattle]
  238. dec a ; is it a trainer battle?
  239. call nz, EnemySendOutFirstMon ; if it is a trainer battle, send out enemy mon
  240. ld c, 40
  241. call DelayFrames
  242. call SaveScreenTilesToBuffer1
  243. .checkAnyPartyAlive
  244. call AnyPartyAlive
  245. ld a, d
  246. and a
  247. jp z, HandlePlayerBlackOut ; jump if no mon is alive
  248. call LoadScreenTilesFromBuffer1
  249. ld a, [wBattleType]
  250. and a ; is it a normal battle?
  251. jp z, .playerSendOutFirstMon ; if so, send out player mon
  252. ; safari zone battle
  253. .displaySafariZoneBattleMenu
  254. call DisplayBattleMenu
  255. ret c ; return if the player ran from battle
  256. ld a, [wActionResultOrTookBattleTurn]
  257. and a ; was the item used successfully?
  258. jr z, .displaySafariZoneBattleMenu ; if not, display the menu again; XXX does this ever jump?
  259. ld a, [wNumSafariBalls]
  260. and a
  261. jr nz, .notOutOfSafariBalls
  262. call LoadScreenTilesFromBuffer1
  263. ld hl, .outOfSafariBallsText
  264. jp PrintText
  265. .notOutOfSafariBalls
  266. callab PrintSafariZoneBattleText
  267. ld a, [wEnemyMonSpeed + 1]
  268. add a
  269. ld b, a ; init b (which is later compared with random value) to (enemy speed % 256) * 2
  270. jp c, EnemyRan ; if (enemy speed % 256) > 127, the enemy runs
  271. ld a, [wSafariBaitFactor]
  272. and a ; is bait factor 0?
  273. jr z, .checkEscapeFactor
  274. ; bait factor is not 0
  275. ; divide b by 4 (making the mon less likely to run)
  276. srl b
  277. srl b
  278. .checkEscapeFactor
  279. ld a, [wSafariEscapeFactor]
  280. and a ; is escape factor 0?
  281. jr z, .compareWithRandomValue
  282. ; escape factor is not 0
  283. ; multiply b by 2 (making the mon more likely to run)
  284. sla b
  285. jr nc, .compareWithRandomValue
  286. ; cap b at 255
  287. ld b, $ff
  288. .compareWithRandomValue
  289. call Random
  290. cp b
  291. jr nc, .checkAnyPartyAlive
  292. jr EnemyRan ; if b was greater than the random value, the enemy runs
  293. .outOfSafariBallsText
  294. TX_FAR _OutOfSafariBallsText
  295. db "@"
  296. .playerSendOutFirstMon
  297. xor a
  298. ld [wWhichPokemon], a
  299. .findFirstAliveMonLoop
  300. call HasMonFainted
  301. jr nz, .foundFirstAliveMon
  302. ; fainted, go to the next one
  303. ld hl, wWhichPokemon
  304. inc [hl]
  305. jr .findFirstAliveMonLoop
  306. .foundFirstAliveMon
  307. ld a, [wWhichPokemon]
  308. ld [wPlayerMonNumber], a
  309. inc a
  310. ld hl, wPartySpecies - 1
  311. ld c, a
  312. ld b, 0
  313. add hl, bc
  314. ld a, [hl] ; species
  315. ld [wcf91], a
  316. ld [wBattleMonSpecies2], a
  317. call LoadScreenTilesFromBuffer1
  318. coord hl, 1, 5
  319. ld a, $9
  320. call SlideTrainerPicOffScreen
  321. call SaveScreenTilesToBuffer1
  322. ld a, [wWhichPokemon]
  323. ld c, a
  324. ld b, FLAG_SET
  325. push bc
  326. ld hl, wPartyGainExpFlags
  327. predef FlagActionPredef
  328. ld hl, wPartyFoughtCurrentEnemyFlags
  329. pop bc
  330. predef FlagActionPredef
  331. call LoadBattleMonFromParty
  332. call LoadScreenTilesFromBuffer1
  333. call SendOutMon
  334. jr MainInBattleLoop
  335. ; wild mon or link battle enemy ran from battle
  336. EnemyRan:
  337. call LoadScreenTilesFromBuffer1
  338. ld a, [wLinkState]
  339. cp LINK_STATE_BATTLING
  340. ld hl, WildRanText
  341. jr nz, .printText
  342. ; link battle
  343. xor a
  344. ld [wBattleResult], a
  345. ld hl, EnemyRanText
  346. .printText
  347. call PrintText
  348. ld a, SFX_RUN
  349. call PlaySoundWaitForCurrent
  350. xor a
  351. ld [H_WHOSETURN], a
  352. jpab AnimationSlideEnemyMonOff
  353. WildRanText:
  354. TX_FAR _WildRanText
  355. db "@"
  356. EnemyRanText:
  357. TX_FAR _EnemyRanText
  358. db "@"
  359. MainInBattleLoop:
  360. call ReadPlayerMonCurHPAndStatus
  361. ld hl, wBattleMonHP
  362. ld a, [hli]
  363. or [hl] ; is battle mon HP 0?
  364. jp z, HandlePlayerMonFainted ; if battle mon HP is 0, jump
  365. ld hl, wEnemyMonHP
  366. ld a, [hli]
  367. or [hl] ; is enemy mon HP 0?
  368. jp z, HandleEnemyMonFainted ; if enemy mon HP is 0, jump
  369. call SaveScreenTilesToBuffer1
  370. xor a
  371. ld [wFirstMonsNotOutYet], a
  372. ld a, [wPlayerBattleStatus2]
  373. and (1 << NEEDS_TO_RECHARGE) | (1 << USING_RAGE) ; check if the player is using Rage or needs to recharge
  374. jr nz, .selectEnemyMove
  375. ; the player is not using Rage and doesn't need to recharge
  376. ld hl, wEnemyBattleStatus1
  377. res FLINCHED, [hl] ; reset flinch bit
  378. ld hl, wPlayerBattleStatus1
  379. res FLINCHED, [hl] ; reset flinch bit
  380. ld a, [hl]
  381. and (1 << THRASHING_ABOUT) | (1 << CHARGING_UP) ; check if the player is thrashing about or charging for an attack
  382. jr nz, .selectEnemyMove ; if so, jump
  383. ; the player is neither thrashing about nor charging for an attack
  384. call DisplayBattleMenu ; show battle menu
  385. ret c ; return if player ran from battle
  386. ld a, [wEscapedFromBattle]
  387. and a
  388. ret nz ; return if pokedoll was used to escape from battle
  389. ld a, [wBattleMonStatus]
  390. and (1 << FRZ) | SLP ; is mon frozen or asleep?
  391. jr nz, .selectEnemyMove ; if so, jump
  392. ld a, [wPlayerBattleStatus1]
  393. and (1 << STORING_ENERGY) | (1 << USING_TRAPPING_MOVE) ; check player is using Bide or using a multi-turn attack like wrap
  394. jr nz, .selectEnemyMove ; if so, jump
  395. ld a, [wEnemyBattleStatus1]
  396. bit USING_TRAPPING_MOVE, a ; check if enemy is using a multi-turn attack like wrap
  397. jr z, .selectPlayerMove ; if not, jump
  398. ; enemy is using a multi-turn attack like wrap, so player is trapped and cannot execute a move
  399. ld a, $ff
  400. ld [wPlayerSelectedMove], a
  401. jr .selectEnemyMove
  402. .selectPlayerMove
  403. ld a, [wActionResultOrTookBattleTurn]
  404. and a ; has the player already used the turn (e.g. by using an item, trying to run or switching pokemon)
  405. jr nz, .selectEnemyMove
  406. ld [wMoveMenuType], a
  407. inc a
  408. ld [wAnimationID], a
  409. xor a
  410. ld [wMenuItemToSwap], a
  411. call MoveSelectionMenu
  412. push af
  413. call LoadScreenTilesFromBuffer1
  414. call DrawHUDsAndHPBars
  415. pop af
  416. jr nz, MainInBattleLoop ; if the player didn't select a move, jump
  417. .selectEnemyMove
  418. call SelectEnemyMove
  419. ld a, [wLinkState]
  420. cp LINK_STATE_BATTLING
  421. jr nz, .noLinkBattle
  422. ; link battle
  423. ld a, [wSerialExchangeNybbleReceiveData]
  424. cp LINKBATTLE_RUN
  425. jp z, EnemyRan
  426. cp LINKBATTLE_STRUGGLE
  427. jr z, .noLinkBattle
  428. cp LINKBATTLE_NO_ACTION
  429. jr z, .noLinkBattle
  430. sub 4
  431. jr c, .noLinkBattle
  432. ; the link battle enemy has switched mons
  433. ld a, [wPlayerBattleStatus1]
  434. bit USING_TRAPPING_MOVE, a ; check if using multi-turn move like Wrap
  435. jr z, .specialMoveNotUsed
  436. ld a, [wPlayerMoveListIndex]
  437. ld hl, wBattleMonMoves
  438. ld c, a
  439. ld b, 0
  440. add hl, bc
  441. ld a, [hl]
  442. cp METRONOME ; a MIRROR MOVE check is missing, might lead to a desync in link battles
  443. ; when combined with multi-turn moves
  444. jr nz, .specialMoveNotUsed
  445. ld [wPlayerSelectedMove], a
  446. .specialMoveNotUsed
  447. callab SwitchEnemyMon
  448. .noLinkBattle
  449. ld a, [wPlayerSelectedMove]
  450. cp QUICK_ATTACK
  451. jr nz, .playerDidNotUseQuickAttack
  452. ld a, [wEnemySelectedMove]
  453. cp QUICK_ATTACK
  454. jr z, .compareSpeed ; if both used Quick Attack
  455. jp .playerMovesFirst ; if player used Quick Attack and enemy didn't
  456. .playerDidNotUseQuickAttack
  457. ld a, [wEnemySelectedMove]
  458. cp QUICK_ATTACK
  459. jr z, .enemyMovesFirst ; if enemy used Quick Attack and player didn't
  460. ld a, [wPlayerSelectedMove]
  461. cp COUNTER
  462. jr nz, .playerDidNotUseCounter
  463. ld a, [wEnemySelectedMove]
  464. cp COUNTER
  465. jr z, .compareSpeed ; if both used Counter
  466. jr .enemyMovesFirst ; if player used Counter and enemy didn't
  467. .playerDidNotUseCounter
  468. ld a, [wEnemySelectedMove]
  469. cp COUNTER
  470. jr z, .playerMovesFirst ; if enemy used Counter and player didn't
  471. .compareSpeed
  472. ld de, wBattleMonSpeed ; player speed value
  473. ld hl, wEnemyMonSpeed ; enemy speed value
  474. ld c, $2
  475. call StringCmp ; compare speed values
  476. jr z, .speedEqual
  477. jr nc, .playerMovesFirst ; if player is faster
  478. jr .enemyMovesFirst ; if enemy is faster
  479. .speedEqual ; 50/50 chance for both players
  480. ld a, [hSerialConnectionStatus]
  481. cp USING_INTERNAL_CLOCK
  482. jr z, .invertOutcome
  483. call BattleRandom
  484. cp $80
  485. jr c, .playerMovesFirst
  486. jr .enemyMovesFirst
  487. .invertOutcome
  488. call BattleRandom
  489. cp $80
  490. jr c, .enemyMovesFirst
  491. jr .playerMovesFirst
  492. .enemyMovesFirst
  493. ld a, $1
  494. ld [H_WHOSETURN], a
  495. callab TrainerAI
  496. jr c, .AIActionUsedEnemyFirst
  497. call ExecuteEnemyMove
  498. ld a, [wEscapedFromBattle]
  499. and a ; was Teleport, Road, or Whirlwind used to escape from battle?
  500. ret nz ; if so, return
  501. ld a, b
  502. and a
  503. jp z, HandlePlayerMonFainted
  504. .AIActionUsedEnemyFirst
  505. call HandlePoisonBurnLeechSeed
  506. jp z, HandleEnemyMonFainted
  507. call DrawHUDsAndHPBars
  508. call ExecutePlayerMove
  509. ld a, [wEscapedFromBattle]
  510. and a ; was Teleport, Road, or Whirlwind used to escape from battle?
  511. ret nz ; if so, return
  512. ld a, b
  513. and a
  514. jp z, HandleEnemyMonFainted
  515. call HandlePoisonBurnLeechSeed
  516. jp z, HandlePlayerMonFainted
  517. call DrawHUDsAndHPBars
  518. call CheckNumAttacksLeft
  519. jp MainInBattleLoop
  520. .playerMovesFirst
  521. call ExecutePlayerMove
  522. ld a, [wEscapedFromBattle]
  523. and a ; was Teleport, Road, or Whirlwind used to escape from battle?
  524. ret nz ; if so, return
  525. ld a, b
  526. and a
  527. jp z, HandleEnemyMonFainted
  528. call HandlePoisonBurnLeechSeed
  529. jp z, HandlePlayerMonFainted
  530. call DrawHUDsAndHPBars
  531. ld a, $1
  532. ld [H_WHOSETURN], a
  533. callab TrainerAI
  534. jr c, .AIActionUsedPlayerFirst
  535. call ExecuteEnemyMove
  536. ld a, [wEscapedFromBattle]
  537. and a ; was Teleport, Road, or Whirlwind used to escape from battle?
  538. ret nz ; if so, return
  539. ld a, b
  540. and a
  541. jp z, HandlePlayerMonFainted
  542. .AIActionUsedPlayerFirst
  543. call HandlePoisonBurnLeechSeed
  544. jp z, HandleEnemyMonFainted
  545. call DrawHUDsAndHPBars
  546. call CheckNumAttacksLeft
  547. jp MainInBattleLoop
  548. HandlePoisonBurnLeechSeed:
  549. ld hl, wBattleMonHP
  550. ld de, wBattleMonStatus
  551. ld a, [H_WHOSETURN]
  552. and a
  553. jr z, .playersTurn
  554. ld hl, wEnemyMonHP
  555. ld de, wEnemyMonStatus
  556. .playersTurn
  557. ld a, [de]
  558. and (1 << BRN) | (1 << PSN)
  559. jr z, .notBurnedOrPoisoned
  560. push hl
  561. ld hl, HurtByPoisonText
  562. ld a, [de]
  563. and 1 << BRN
  564. jr z, .poisoned
  565. ld hl, HurtByBurnText
  566. .poisoned
  567. call PrintText
  568. xor a
  569. ld [wAnimationType], a
  570. ld a, BURN_PSN_ANIM
  571. call PlayMoveAnimation ; play burn/poison animation
  572. pop hl
  573. call HandlePoisonBurnLeechSeed_DecreaseOwnHP
  574. .notBurnedOrPoisoned
  575. ld de, wPlayerBattleStatus2
  576. ld a, [H_WHOSETURN]
  577. and a
  578. jr z, .playersTurn2
  579. ld de, wEnemyBattleStatus2
  580. .playersTurn2
  581. ld a, [de]
  582. add a
  583. jr nc, .notLeechSeeded
  584. push hl
  585. ld a, [H_WHOSETURN]
  586. push af
  587. xor $1
  588. ld [H_WHOSETURN], a
  589. xor a
  590. ld [wAnimationType], a
  591. ld a, ABSORB
  592. call PlayMoveAnimation ; play leech seed animation (from opposing mon)
  593. pop af
  594. ld [H_WHOSETURN], a
  595. pop hl
  596. call HandlePoisonBurnLeechSeed_DecreaseOwnHP
  597. call HandlePoisonBurnLeechSeed_IncreaseEnemyHP
  598. push hl
  599. ld hl, HurtByLeechSeedText
  600. call PrintText
  601. pop hl
  602. .notLeechSeeded
  603. ld a, [hli]
  604. or [hl]
  605. ret nz ; test if fainted
  606. call DrawHUDsAndHPBars
  607. ld c, 20
  608. call DelayFrames
  609. xor a
  610. ret
  611. HurtByPoisonText:
  612. TX_FAR _HurtByPoisonText
  613. db "@"
  614. HurtByBurnText:
  615. TX_FAR _HurtByBurnText
  616. db "@"
  617. HurtByLeechSeedText:
  618. TX_FAR _HurtByLeechSeedText
  619. db "@"
  620. ; decreases the mon's current HP by 1/16 of the Max HP (multiplied by number of toxic ticks if active)
  621. ; note that the toxic ticks are considered even if the damage is not poison (hence the Leech Seed glitch)
  622. ; hl: HP pointer
  623. ; bc (out): total damage
  624. HandlePoisonBurnLeechSeed_DecreaseOwnHP:
  625. push hl
  626. push hl
  627. ld bc, $e ; skip to max HP
  628. add hl, bc
  629. ld a, [hli] ; load max HP
  630. ld [wHPBarMaxHP+1], a
  631. ld b, a
  632. ld a, [hl]
  633. ld [wHPBarMaxHP], a
  634. ld c, a
  635. srl b
  636. rr c
  637. srl b
  638. rr c
  639. srl c
  640. srl c ; c = max HP/16 (assumption: HP < 1024)
  641. ld a, c
  642. and a
  643. jr nz, .nonZeroDamage
  644. inc c ; damage is at least 1
  645. .nonZeroDamage
  646. ld hl, wPlayerBattleStatus3
  647. ld de, wPlayerToxicCounter
  648. ld a, [H_WHOSETURN]
  649. and a
  650. jr z, .playersTurn
  651. ld hl, wEnemyBattleStatus3
  652. ld de, wEnemyToxicCounter
  653. .playersTurn
  654. bit BADLY_POISONED, [hl]
  655. jr z, .noToxic
  656. ld a, [de] ; increment toxic counter
  657. inc a
  658. ld [de], a
  659. ld hl, $0000
  660. .toxicTicksLoop
  661. add hl, bc
  662. dec a
  663. jr nz, .toxicTicksLoop
  664. ld b, h ; bc = damage * toxic counter
  665. ld c, l
  666. .noToxic
  667. pop hl
  668. inc hl
  669. ld a, [hl] ; subtract total damage from current HP
  670. ld [wHPBarOldHP], a
  671. sub c
  672. ld [hld], a
  673. ld [wHPBarNewHP], a
  674. ld a, [hl]
  675. ld [wHPBarOldHP+1], a
  676. sbc b
  677. ld [hl], a
  678. ld [wHPBarNewHP+1], a
  679. jr nc, .noOverkill
  680. xor a ; overkill: zero HP
  681. ld [hli], a
  682. ld [hl], a
  683. ld [wHPBarNewHP], a
  684. ld [wHPBarNewHP+1], a
  685. .noOverkill
  686. call UpdateCurMonHPBar
  687. pop hl
  688. ret
  689. ; adds bc to enemy HP
  690. ; bc isn't updated if HP subtracted was capped to prevent overkill
  691. HandlePoisonBurnLeechSeed_IncreaseEnemyHP:
  692. push hl
  693. ld hl, wEnemyMonMaxHP
  694. ld a, [H_WHOSETURN]
  695. and a
  696. jr z, .playersTurn
  697. ld hl, wBattleMonMaxHP
  698. .playersTurn
  699. ld a, [hli]
  700. ld [wHPBarMaxHP+1], a
  701. ld a, [hl]
  702. ld [wHPBarMaxHP], a
  703. ld de, wBattleMonHP - wBattleMonMaxHP
  704. add hl, de ; skip back from max hp to current hp
  705. ld a, [hl]
  706. ld [wHPBarOldHP], a ; add bc to current HP
  707. add c
  708. ld [hld], a
  709. ld [wHPBarNewHP], a
  710. ld a, [hl]
  711. ld [wHPBarOldHP+1], a
  712. adc b
  713. ld [hli], a
  714. ld [wHPBarNewHP+1], a
  715. ld a, [wHPBarMaxHP]
  716. ld c, a
  717. ld a, [hld]
  718. sub c
  719. ld a, [wHPBarMaxHP+1]
  720. ld b, a
  721. ld a, [hl]
  722. sbc b
  723. jr c, .noOverfullHeal
  724. ld a, b ; overfull heal, set HP to max HP
  725. ld [hli], a
  726. ld [wHPBarNewHP+1], a
  727. ld a, c
  728. ld [hl], a
  729. ld [wHPBarNewHP], a
  730. .noOverfullHeal
  731. ld a, [H_WHOSETURN]
  732. xor $1
  733. ld [H_WHOSETURN], a
  734. call UpdateCurMonHPBar
  735. ld a, [H_WHOSETURN]
  736. xor $1
  737. ld [H_WHOSETURN], a
  738. pop hl
  739. ret
  740. UpdateCurMonHPBar:
  741. coord hl, 10, 9 ; tile pointer to player HP bar
  742. ld a, [H_WHOSETURN]
  743. and a
  744. ld a, $1
  745. jr z, .playersTurn
  746. coord hl, 2, 2 ; tile pointer to enemy HP bar
  747. xor a
  748. .playersTurn
  749. push bc
  750. ld [wHPBarType], a
  751. predef UpdateHPBar2
  752. pop bc
  753. ret
  754. CheckNumAttacksLeft:
  755. ld a, [wPlayerNumAttacksLeft]
  756. and a
  757. jr nz, .checkEnemy
  758. ; player has 0 attacks left
  759. ld hl, wPlayerBattleStatus1
  760. res USING_TRAPPING_MOVE, [hl] ; player not using multi-turn attack like wrap any more
  761. .checkEnemy
  762. ld a, [wEnemyNumAttacksLeft]
  763. and a
  764. ret nz
  765. ; enemy has 0 attacks left
  766. ld hl, wEnemyBattleStatus1
  767. res USING_TRAPPING_MOVE, [hl] ; enemy not using multi-turn attack like wrap any more
  768. ret
  769. HandleEnemyMonFainted:
  770. xor a
  771. ld [wInHandlePlayerMonFainted], a
  772. call FaintEnemyPokemon
  773. call AnyPartyAlive
  774. ld a, d
  775. and a
  776. jp z, HandlePlayerBlackOut ; if no party mons are alive, the player blacks out
  777. ld hl, wBattleMonHP
  778. ld a, [hli]
  779. or [hl] ; is battle mon HP zero?
  780. call nz, DrawPlayerHUDAndHPBar ; if battle mon HP is not zero, draw player HD and HP bar
  781. ld a, [wIsInBattle]
  782. dec a
  783. ret z ; return if it's a wild battle
  784. call AnyEnemyPokemonAliveCheck
  785. jp z, TrainerBattleVictory
  786. ld hl, wBattleMonHP
  787. ld a, [hli]
  788. or [hl] ; does battle mon have 0 HP?
  789. jr nz, .skipReplacingBattleMon ; if not, skip replacing battle mon
  790. call DoUseNextMonDialogue ; this call is useless in a trainer battle. it shouldn't be here
  791. ret c
  792. call ChooseNextMon
  793. .skipReplacingBattleMon
  794. ld a, $1
  795. ld [wActionResultOrTookBattleTurn], a
  796. call ReplaceFaintedEnemyMon
  797. jp z, EnemyRan
  798. xor a
  799. ld [wActionResultOrTookBattleTurn], a
  800. jp MainInBattleLoop
  801. FaintEnemyPokemon:
  802. call ReadPlayerMonCurHPAndStatus
  803. ld a, [wIsInBattle]
  804. dec a
  805. jr z, .wild
  806. ld a, [wEnemyMonPartyPos]
  807. ld hl, wEnemyMon1HP
  808. ld bc, wEnemyMon2 - wEnemyMon1
  809. call AddNTimes
  810. xor a
  811. ld [hli], a
  812. ld [hl], a
  813. .wild
  814. ld hl, wPlayerBattleStatus1
  815. res ATTACKING_MULTIPLE_TIMES, [hl]
  816. ; Bug. This only zeroes the high byte of the player's accumulated damage,
  817. ; setting the accumulated damage to itself mod 256 instead of 0 as was probably
  818. ; intended. That alone is problematic, but this mistake has another more severe
  819. ; effect. This function's counterpart for when the player mon faints,
  820. ; RemoveFaintedPlayerMon, zeroes both the high byte and the low byte. In a link
  821. ; battle, the other player's Game Boy will call that function in response to
  822. ; the enemy mon (the player mon from the other side's perspective) fainting,
  823. ; and the states of the two Game Boys will go out of sync unless the damage
  824. ; was congruent to 0 modulo 256.
  825. xor a
  826. ld [wPlayerBideAccumulatedDamage], a
  827. ld hl, wEnemyStatsToDouble ; clear enemy statuses
  828. ld [hli], a
  829. ld [hli], a
  830. ld [hli], a
  831. ld [hli], a
  832. ld [hl], a
  833. ld [wEnemyDisabledMove], a
  834. ld [wEnemyDisabledMoveNumber], a
  835. ld [wEnemyMonMinimized], a
  836. ld hl, wPlayerUsedMove
  837. ld [hli], a
  838. ld [hl], a
  839. coord hl, 12, 5
  840. coord de, 12, 6
  841. call SlideDownFaintedMonPic
  842. coord hl, 0, 0
  843. lb bc, 4, 11
  844. call ClearScreenArea
  845. ld a, [wIsInBattle]
  846. dec a
  847. jr z, .wild_win
  848. xor a
  849. ld [wFrequencyModifier], a
  850. ld [wTempoModifier], a
  851. ld a, SFX_FAINT_FALL
  852. call PlaySoundWaitForCurrent
  853. .sfxwait
  854. ld a, [wChannelSoundIDs + Ch4]
  855. cp SFX_FAINT_FALL
  856. jr z, .sfxwait
  857. ld a, SFX_FAINT_THUD
  858. call PlaySound
  859. call WaitForSoundToFinish
  860. jr .sfxplayed
  861. .wild_win
  862. call EndLowHealthAlarm
  863. ld a, MUSIC_DEFEATED_WILD_MON
  864. call PlayBattleVictoryMusic
  865. .sfxplayed
  866. ; bug: win sfx is played for wild battles before checking for player mon HP
  867. ; this can lead to odd scenarios where both player and enemy faint, as the win sfx plays yet the player never won the battle
  868. ld hl, wBattleMonHP
  869. ld a, [hli]
  870. or [hl]
  871. jr nz, .playermonnotfaint
  872. ld a, [wInHandlePlayerMonFainted]
  873. and a ; was this called by HandlePlayerMonFainted?
  874. jr nz, .playermonnotfaint ; if so, don't call RemoveFaintedPlayerMon twice
  875. call RemoveFaintedPlayerMon
  876. .playermonnotfaint
  877. call AnyPartyAlive
  878. ld a, d
  879. and a
  880. ret z
  881. ld hl, EnemyMonFaintedText
  882. call PrintText
  883. call PrintEmptyString
  884. call SaveScreenTilesToBuffer1
  885. xor a
  886. ld [wBattleResult], a
  887. ld b, EXP_ALL
  888. call IsItemInBag
  889. push af
  890. jr z, .giveExpToMonsThatFought ; if no exp all, then jump
  891. ; the player has exp all
  892. ; first, we halve the values that determine exp gain
  893. ; the enemy mon base stats are added to stat exp, so they are halved
  894. ; the base exp (which determines normal exp) is also halved
  895. ld hl, wEnemyMonBaseStats
  896. ld b, $7
  897. .halveExpDataLoop
  898. srl [hl]
  899. inc hl
  900. dec b
  901. jr nz, .halveExpDataLoop
  902. ; give exp (divided evenly) to the mons that actually fought in battle against the enemy mon that has fainted
  903. ; if exp all is in the bag, this will be only be half of the stat exp and normal exp, due to the above loop
  904. .giveExpToMonsThatFought
  905. xor a
  906. ld [wBoostExpByExpAll], a
  907. callab GainExperience
  908. pop af
  909. ret z ; return if no exp all
  910. ; the player has exp all
  911. ; now, set the gain exp flag for every party member
  912. ; half of the total stat exp and normal exp will divided evenly amongst every party member
  913. ld a, $1
  914. ld [wBoostExpByExpAll], a
  915. ld a, [wPartyCount]
  916. ld b, 0
  917. .gainExpFlagsLoop
  918. scf
  919. rl b
  920. dec a
  921. jr nz, .gainExpFlagsLoop
  922. ld a, b
  923. ld [wPartyGainExpFlags], a
  924. jpab GainExperience
  925. EnemyMonFaintedText:
  926. TX_FAR _EnemyMonFaintedText
  927. db "@"
  928. EndLowHealthAlarm:
  929. ; This function is called when the player has the won the battle. It turns off
  930. ; the low health alarm and prevents it from reactivating until the next battle.
  931. xor a
  932. ld [wLowHealthAlarm], a ; turn off low health alarm
  933. ld [wChannelSoundIDs + Ch4], a
  934. inc a
  935. ld [wLowHealthAlarmDisabled], a ; prevent it from reactivating
  936. ret
  937. AnyEnemyPokemonAliveCheck:
  938. ld a, [wEnemyPartyCount]
  939. ld b, a
  940. xor a
  941. ld hl, wEnemyMon1HP
  942. ld de, wEnemyMon2 - wEnemyMon1
  943. .nextPokemon
  944. or [hl]
  945. inc hl
  946. or [hl]
  947. dec hl
  948. add hl, de
  949. dec b
  950. jr nz, .nextPokemon
  951. and a
  952. ret
  953. ; stores whether enemy ran in Z flag
  954. ReplaceFaintedEnemyMon:
  955. ld hl, wEnemyHPBarColor
  956. ld e, $30
  957. call GetBattleHealthBarColor
  958. callab DrawEnemyPokeballs
  959. ld a, [wLinkState]
  960. cp LINK_STATE_BATTLING
  961. jr nz, .notLinkBattle
  962. ; link battle
  963. call LinkBattleExchangeData
  964. ld a, [wSerialExchangeNybbleReceiveData]
  965. cp LINKBATTLE_RUN
  966. ret z
  967. call LoadScreenTilesFromBuffer1
  968. .notLinkBattle
  969. call EnemySendOut
  970. xor a
  971. ld [wEnemyMoveNum], a
  972. ld [wActionResultOrTookBattleTurn], a
  973. ld [wAILayer2Encouragement], a
  974. inc a ; reset Z flag
  975. ret
  976. TrainerBattleVictory:
  977. call EndLowHealthAlarm
  978. ld b, MUSIC_DEFEATED_GYM_LEADER
  979. ld a, [wGymLeaderNo]
  980. and a
  981. jr nz, .gymleader
  982. ld b, MUSIC_DEFEATED_TRAINER
  983. .gymleader
  984. ld a, [wTrainerClass]
  985. cp SONY3 ; final battle against rival
  986. jr nz, .notrival
  987. ld b, MUSIC_DEFEATED_GYM_LEADER
  988. ld hl, wFlags_D733
  989. set 1, [hl]
  990. .notrival
  991. ld a, [wLinkState]
  992. cp LINK_STATE_BATTLING
  993. ld a, b
  994. call nz, PlayBattleVictoryMusic
  995. ld hl, TrainerDefeatedText
  996. call PrintText
  997. ld a, [wLinkState]
  998. cp LINK_STATE_BATTLING
  999. ret z
  1000. call ScrollTrainerPicAfterBattle
  1001. ld c, 40
  1002. call DelayFrames
  1003. call PrintEndBattleText
  1004. ; win money
  1005. ld hl, MoneyForWinningText
  1006. call PrintText
  1007. ld de, wPlayerMoney + 2
  1008. ld hl, wAmountMoneyWon + 2
  1009. ld c, $3
  1010. predef_jump AddBCDPredef
  1011. MoneyForWinningText:
  1012. TX_FAR _MoneyForWinningText
  1013. db "@"
  1014. TrainerDefeatedText:
  1015. TX_FAR _TrainerDefeatedText
  1016. db "@"
  1017. PlayBattleVictoryMusic:
  1018. push af
  1019. ld a, $ff
  1020. ld [wNewSoundID], a
  1021. call PlaySoundWaitForCurrent
  1022. ld c, BANK(Music_DefeatedTrainer)
  1023. pop af
  1024. call PlayMusic
  1025. jp Delay3
  1026. HandlePlayerMonFainted:
  1027. ld a, 1
  1028. ld [wInHandlePlayerMonFainted], a
  1029. call RemoveFaintedPlayerMon
  1030. call AnyPartyAlive ; test if any more mons are alive
  1031. ld a, d
  1032. and a
  1033. jp z, HandlePlayerBlackOut
  1034. ld hl, wEnemyMonHP
  1035. ld a, [hli]
  1036. or [hl] ; is enemy mon's HP 0?
  1037. jr nz, .doUseNextMonDialogue ; if not, jump
  1038. ; the enemy mon has 0 HP
  1039. call FaintEnemyPokemon
  1040. ld a, [wIsInBattle]
  1041. dec a
  1042. ret z ; if wild encounter, battle is over
  1043. call AnyEnemyPokemonAliveCheck
  1044. jp z, TrainerBattleVictory
  1045. .doUseNextMonDialogue
  1046. call DoUseNextMonDialogue
  1047. ret c ; return if the player ran from battle
  1048. call ChooseNextMon
  1049. jp nz, MainInBattleLoop ; if the enemy mon has more than 0 HP, go back to battle loop
  1050. ; the enemy mon has 0 HP
  1051. ld a, $1
  1052. ld [wActionResultOrTookBattleTurn], a
  1053. call ReplaceFaintedEnemyMon
  1054. jp z, EnemyRan ; if enemy ran from battle rather than sending out another mon, jump
  1055. xor a
  1056. ld [wActionResultOrTookBattleTurn], a
  1057. jp MainInBattleLoop
  1058. ; resets flags, slides mon's pic down, plays cry, and prints fainted message
  1059. RemoveFaintedPlayerMon:
  1060. ld a, [wPlayerMonNumber]
  1061. ld c, a
  1062. ld hl, wPartyGainExpFlags
  1063. ld b, FLAG_RESET
  1064. predef FlagActionPredef ; clear gain exp flag for fainted mon
  1065. ld hl, wEnemyBattleStatus1
  1066. res 2, [hl] ; reset "attacking multiple times" flag
  1067. ld a, [wLowHealthAlarm]
  1068. bit 7, a ; skip sound flag (red bar (?))
  1069. jr z, .skipWaitForSound
  1070. ld a, $ff
  1071. ld [wLowHealthAlarm], a ;disable low health alarm
  1072. call WaitForSoundToFinish
  1073. .skipWaitForSound
  1074. ; a is 0, so this zeroes the enemy's accumulated damage.
  1075. ld hl, wEnemyBideAccumulatedDamage
  1076. ld [hli], a
  1077. ld [hl], a
  1078. ld [wBattleMonStatus], a
  1079. call ReadPlayerMonCurHPAndStatus
  1080. coord hl, 9, 7
  1081. lb bc, 5, 11
  1082. call ClearScreenArea
  1083. coord hl, 1, 10
  1084. coord de, 1, 11
  1085. call SlideDownFaintedMonPic
  1086. ld a, $1
  1087. ld [wBattleResult], a
  1088. ; When the player mon and enemy mon faint at the same time and the fact that the
  1089. ; enemy mon has fainted is detected first (e.g. when the player mon knocks out
  1090. ; the enemy mon using a move with recoil and faints due to the recoil), don't
  1091. ; play the player mon's cry or show the "[player mon] fainted!" message.
  1092. ld a, [wInHandlePlayerMonFainted]
  1093. and a ; was this called by HandleEnemyMonFainted?
  1094. ret z ; if so, return
  1095. ld a, [wBattleMonSpecies]
  1096. call PlayCry
  1097. ld hl, PlayerMonFaintedText
  1098. jp PrintText
  1099. PlayerMonFaintedText:
  1100. TX_FAR _PlayerMonFaintedText
  1101. db "@"
  1102. ; asks if you want to use next mon
  1103. ; stores whether you ran in C flag
  1104. DoUseNextMonDialogue:
  1105. call PrintEmptyString
  1106. call SaveScreenTilesToBuffer1
  1107. ld a, [wIsInBattle]
  1108. and a
  1109. dec a
  1110. ret nz ; return if it's a trainer battle
  1111. ld hl, UseNextMonText
  1112. call PrintText
  1113. .displayYesNoBox
  1114. coord hl, 13, 9
  1115. lb bc, 10, 14
  1116. ld a, TWO_OPTION_MENU
  1117. ld [wTextBoxID], a
  1118. call DisplayTextBoxID
  1119. ld a, [wMenuExitMethod]
  1120. cp CHOSE_SECOND_ITEM ; did the player choose NO?
  1121. jr z, .tryRunning ; if the player chose NO, try running
  1122. and a ; reset carry
  1123. ret
  1124. .tryRunning
  1125. ld a, [wCurrentMenuItem]
  1126. and a
  1127. jr z, .displayYesNoBox ; xxx when does this happen?
  1128. ld hl, wPartyMon1Speed
  1129. ld de, wEnemyMonSpeed
  1130. jp TryRunningFromBattle
  1131. UseNextMonText:
  1132. TX_FAR _UseNextMonText
  1133. db "@"
  1134. ; choose next player mon to send out
  1135. ; stores whether enemy mon has no HP left in Z flag
  1136. ChooseNextMon:
  1137. ld a, BATTLE_PARTY_MENU
  1138. ld [wPartyMenuTypeOrMessageID], a
  1139. call DisplayPartyMenu
  1140. .checkIfMonChosen
  1141. jr nc, .monChosen
  1142. .goBackToPartyMenu
  1143. call GoBackToPartyMenu
  1144. jr .checkIfMonChosen
  1145. .monChosen
  1146. call HasMonFainted
  1147. jr z, .goBackToPartyMenu ; if mon fainted, you have to choose another
  1148. ld a, [wLinkState]
  1149. cp LINK_STATE_BATTLING
  1150. jr nz, .notLinkBattle
  1151. inc a
  1152. ld [wActionResultOrTookBattleTurn], a
  1153. call LinkBattleExchangeData
  1154. .notLinkBattle
  1155. xor a
  1156. ld [wActionResultOrTookBattleTurn], a
  1157. call ClearSprites
  1158. ld a, [wWhichPokemon]
  1159. ld [wPlayerMonNumber], a
  1160. ld c, a
  1161. ld hl, wPartyGainExpFlags
  1162. ld b, FLAG_SET
  1163. push bc
  1164. predef FlagActionPredef
  1165. pop bc
  1166. ld hl, wPartyFoughtCurrentEnemyFlags
  1167. predef FlagActionPredef
  1168. call LoadBattleMonFromParty
  1169. call GBPalWhiteOut
  1170. call LoadHudTilePatterns
  1171. call LoadScreenTilesFromBuffer1
  1172. call RunDefaultPaletteCommand
  1173. call GBPalNormal
  1174. call SendOutMon
  1175. ld hl, wEnemyMonHP
  1176. ld a, [hli]
  1177. or [hl]
  1178. ret
  1179. ; called when player is out of usable mons.
  1180. ; prints appropriate lose message, sets carry flag if player blacked out (special case for initial rival fight)
  1181. HandlePlayerBlackOut:
  1182. ld a, [wLinkState]
  1183. cp LINK_STATE_BATTLING
  1184. jr z, .notSony1Battle
  1185. ld a, [wCurOpponent]
  1186. cp OPP_SONY1
  1187. jr nz, .notSony1Battle
  1188. coord hl, 0, 0 ; sony 1 battle
  1189. lb bc, 8, 21
  1190. call ClearScreenArea
  1191. call ScrollTrainerPicAfterBattle
  1192. ld c, 40
  1193. call DelayFrames
  1194. ld hl, Sony1WinText
  1195. call PrintText
  1196. ld a, [wCurMap]
  1197. cp OAKS_LAB
  1198. ret z ; starter battle in oak's lab: don't black out
  1199. .notSony1Battle
  1200. ld b, SET_PAL_BATTLE_BLACK
  1201. call RunPaletteCommand
  1202. ld hl, PlayerBlackedOutText2
  1203. ld a, [wLinkState]
  1204. cp LINK_STATE_BATTLING
  1205. jr nz, .noLinkBattle
  1206. ld hl, LinkBattleLostText
  1207. .noLinkBattle
  1208. call PrintText
  1209. ld a, [wd732]
  1210. res 5, a
  1211. ld [wd732], a
  1212. call ClearScreen
  1213. scf
  1214. ret
  1215. Sony1WinText:
  1216. TX_FAR _Sony1WinText
  1217. db "@"
  1218. PlayerBlackedOutText2:
  1219. TX_FAR _PlayerBlackedOutText2
  1220. db "@"
  1221. LinkBattleLostText:
  1222. TX_FAR _LinkBattleLostText
  1223. db "@"
  1224. ; slides pic of fainted mon downwards until it disappears
  1225. ; bug: when this is called, [H_AUTOBGTRANSFERENABLED] is non-zero, so there is screen tearing
  1226. SlideDownFaintedMonPic:
  1227. ld a, [wd730]
  1228. push af
  1229. set 6, a
  1230. ld [wd730], a
  1231. ld b, 7 ; number of times to slide
  1232. .slideStepLoop ; each iteration, the mon is slid down one row
  1233. push bc
  1234. push de
  1235. push hl
  1236. ld b, 6 ; number of rows
  1237. .rowLoop
  1238. push bc
  1239. push hl
  1240. push de
  1241. ld bc, $7
  1242. call CopyData
  1243. pop de
  1244. pop hl
  1245. ld bc, -SCREEN_WIDTH
  1246. add hl, bc
  1247. push hl
  1248. ld h, d
  1249. ld l, e
  1250. add hl, bc
  1251. ld d, h
  1252. ld e, l
  1253. pop hl
  1254. pop bc
  1255. dec b
  1256. jr nz, .rowLoop
  1257. ld bc, SCREEN_WIDTH
  1258. add hl, bc
  1259. ld de, SevenSpacesText
  1260. call PlaceString
  1261. ld c, 2
  1262. call DelayFrames
  1263. pop hl
  1264. pop de
  1265. pop bc
  1266. dec b
  1267. jr nz, .slideStepLoop
  1268. pop af
  1269. ld [wd730], a
  1270. ret
  1271. SevenSpacesText:
  1272. db " @"
  1273. ; slides the player or enemy trainer off screen
  1274. ; a is the number of tiles to slide it horizontally (always 9 for the player trainer or 8 for the enemy trainer)
  1275. ; if a is 8, the slide is to the right, else it is to the left
  1276. ; bug: when this is called, [H_AUTOBGTRANSFERENABLED] is non-zero, so there is screen tearing
  1277. SlideTrainerPicOffScreen:
  1278. ld [hSlideAmount], a
  1279. ld c, a
  1280. .slideStepLoop ; each iteration, the trainer pic is slid one tile left/right
  1281. push bc
  1282. push hl
  1283. ld b, 7 ; number of rows
  1284. .rowLoop
  1285. push hl
  1286. ld a, [hSlideAmount]
  1287. ld c, a
  1288. .columnLoop
  1289. ld a, [hSlideAmount]
  1290. cp 8
  1291. jr z, .slideRight
  1292. .slideLeft ; slide player sprite off screen
  1293. ld a, [hld]
  1294. ld [hli], a
  1295. inc hl
  1296. jr .nextColumn
  1297. .slideRight ; slide enemy trainer sprite off screen
  1298. ld a, [hli]
  1299. ld [hld], a
  1300. dec hl
  1301. .nextColumn
  1302. dec c
  1303. jr nz, .columnLoop
  1304. pop hl
  1305. ld de, 20
  1306. add hl, de
  1307. dec b
  1308. jr nz, .rowLoop
  1309. ld c, 2
  1310. call DelayFrames
  1311. pop hl
  1312. pop bc
  1313. dec c
  1314. jr nz, .slideStepLoop
  1315. ret
  1316. ; send out a trainer's mon
  1317. EnemySendOut:
  1318. ld hl, wPartyGainExpFlags
  1319. xor a
  1320. ld [hl], a
  1321. ld a, [wPlayerMonNumber]
  1322. ld c, a
  1323. ld b, FLAG_SET
  1324. push bc
  1325. predef FlagActionPredef
  1326. ld hl, wPartyFoughtCurrentEnemyFlags
  1327. xor a
  1328. ld [hl], a
  1329. pop bc
  1330. predef FlagActionPredef
  1331. ; don't change wPartyGainExpFlags or wPartyFoughtCurrentEnemyFlags
  1332. EnemySendOutFirstMon:
  1333. xor a
  1334. ld hl, wEnemyStatsToDouble ; clear enemy statuses
  1335. ld [hli], a
  1336. ld [hli], a
  1337. ld [hli], a
  1338. ld [hli], a
  1339. ld [hl], a
  1340. ld [wEnemyDisabledMove], a
  1341. ld [wEnemyDisabledMoveNumber], a
  1342. ld [wEnemyMonMinimized], a
  1343. ld hl, wPlayerUsedMove
  1344. ld [hli], a
  1345. ld [hl], a
  1346. dec a
  1347. ld [wAICount], a
  1348. ld hl, wPlayerBattleStatus1
  1349. res 5, [hl]
  1350. coord hl, 18, 0
  1351. ld a, 8
  1352. call SlideTrainerPicOffScreen
  1353. call PrintEmptyString
  1354. call SaveScreenTilesToBuffer1
  1355. ld a, [wLinkState]
  1356. cp LINK_STATE_BATTLING
  1357. jr nz, .next
  1358. ld a, [wSerialExchangeNybbleReceiveData]
  1359. sub 4
  1360. ld [wWhichPokemon], a
  1361. jr .next3
  1362. .next
  1363. ld b, $FF
  1364. .next2
  1365. inc b
  1366. ld a, [wEnemyMonPartyPos]
  1367. cp b
  1368. jr z, .next2
  1369. ld hl, wEnemyMon1
  1370. ld a, b
  1371. ld [wWhichPokemon], a
  1372. push bc
  1373. ld bc, wEnemyMon2 - wEnemyMon1
  1374. call AddNTimes
  1375. pop bc
  1376. inc hl
  1377. ld a, [hli]
  1378. ld c, a
  1379. ld a, [hl]
  1380. or c
  1381. jr z, .next2
  1382. .next3
  1383. ld a, [wWhichPokemon]
  1384. ld hl, wEnemyMon1Level
  1385. ld bc, wEnemyMon2 - wEnemyMon1
  1386. call AddNTimes
  1387. ld a, [hl]
  1388. ld [wCurEnemyLVL], a
  1389. ld a, [wWhichPokemon]
  1390. inc a
  1391. ld hl, wEnemyPartyCount
  1392. ld c, a
  1393. ld b, 0
  1394. add hl, bc
  1395. ld a, [hl]
  1396. ld [wEnemyMonSpecies2], a
  1397. ld [wcf91], a
  1398. call LoadEnemyMonData
  1399. ld hl, wEnemyMonHP
  1400. ld a, [hli]
  1401. ld [wLastSwitchInEnemyMonHP], a
  1402. ld a, [hl]
  1403. ld [wLastSwitchInEnemyMonHP + 1], a
  1404. ld a, 1
  1405. ld [wCurrentMenuItem], a
  1406. ld a, [wFirstMonsNotOutYet]
  1407. dec a
  1408. jr z, .next4
  1409. ld a, [wPartyCount]
  1410. dec a
  1411. jr z, .next4
  1412. ld a, [wLinkState]
  1413. cp LINK_STATE_BATTLING
  1414. jr z, .next4
  1415. ld a, [wOptions]
  1416. bit 6, a
  1417. jr nz, .next4
  1418. ld hl, TrainerAboutToUseText
  1419. call PrintText
  1420. coord hl, 0, 7
  1421. lb bc, 8, 1
  1422. ld a, TWO_OPTION_MENU
  1423. ld [wTextBoxID], a
  1424. call DisplayTextBoxID
  1425. ld a, [wCurrentMenuItem]
  1426. and a
  1427. jr nz, .next4
  1428. ld a, BATTLE_PARTY_MENU
  1429. ld [wPartyMenuTypeOrMessageID], a
  1430. call DisplayPartyMenu
  1431. .next9
  1432. ld a, 1
  1433. ld [wCurrentMenuItem], a
  1434. jr c, .next7
  1435. ld hl, wPlayerMonNumber
  1436. ld a, [wWhichPokemon]
  1437. cp [hl]
  1438. jr nz, .next6
  1439. ld hl, AlreadyOutText
  1440. call PrintText
  1441. .next8
  1442. call GoBackToPartyMenu
  1443. jr .next9
  1444. .next6
  1445. call HasMonFainted
  1446. jr z, .next8
  1447. xor a
  1448. ld [wCurrentMenuItem], a
  1449. .next7
  1450. call GBPalWhiteOut
  1451. call LoadHudTilePatterns
  1452. call LoadScreenTilesFromBuffer1
  1453. .next4
  1454. call ClearSprites
  1455. coord hl, 0, 0
  1456. lb bc, 4, 11
  1457. call ClearScreenArea
  1458. ld b, SET_PAL_BATTLE
  1459. call RunPaletteCommand
  1460. call GBPalNormal
  1461. ld hl, TrainerSentOutText
  1462. call PrintText
  1463. ld a, [wEnemyMonSpecies2]
  1464. ld [wcf91], a
  1465. ld [wd0b5], a
  1466. call GetMonHeader
  1467. ld de, vFrontPic
  1468. call LoadMonFrontSprite
  1469. ld a, -$31
  1470. ld [hStartTileID], a
  1471. coord hl, 15, 6
  1472. predef AnimateSendingOutMon
  1473. ld a, [wEnemyMonSpecies2]
  1474. call PlayCry
  1475. call DrawEnemyHUDAndHPBar
  1476. ld a, [wCurrentMenuItem]
  1477. and a
  1478. ret nz
  1479. xor a
  1480. ld [wPartyGainExpFlags], a
  1481. ld [wPartyFoughtCurrentEnemyFlags], a
  1482. call SaveScreenTilesToBuffer1
  1483. jp SwitchPlayerMon
  1484. TrainerAboutToUseText:
  1485. TX_FAR _TrainerAboutToUseText
  1486. db "@"
  1487. TrainerSentOutText:
  1488. TX_FAR _TrainerSentOutText
  1489. db "@"
  1490. ; tests if the player has any pokemon that are not fainted
  1491. ; sets d = 0 if all fainted, d != 0 if some mons are still alive
  1492. AnyPartyAlive:
  1493. ld a, [wPartyCount]
  1494. ld e, a
  1495. xor a
  1496. ld hl, wPartyMon1HP
  1497. ld bc, wPartyMon2 - wPartyMon1 - 1
  1498. .partyMonsLoop
  1499. or [hl]
  1500. inc hl
  1501. or [hl]
  1502. add hl, bc
  1503. dec e
  1504. jr nz, .partyMonsLoop
  1505. ld d, a
  1506. ret
  1507. ; tests if player mon has fainted
  1508. ; stores whether mon has fainted in Z flag
  1509. HasMonFainted:
  1510. ld a, [wWhichPokemon]
  1511. ld hl, wPartyMon1HP
  1512. ld bc, wPartyMon2 - wPartyMon1
  1513. call AddNTimes
  1514. ld a, [hli]
  1515. or [hl]
  1516. ret nz
  1517. ld a, [wFirstMonsNotOutYet]
  1518. and a
  1519. jr nz, .done
  1520. ld hl, NoWillText
  1521. call PrintText
  1522. .done
  1523. xor a
  1524. ret
  1525. NoWillText:
  1526. TX_FAR _NoWillText
  1527. db "@"
  1528. ; try to run from battle (hl = player speed, de = enemy speed)
  1529. ; stores whether the attempt was successful in carry flag
  1530. TryRunningFromBattle:
  1531. call IsGhostBattle
  1532. jp z, .canEscape ; jump if it's a ghost battle
  1533. ld a, [wBattleType]
  1534. cp BATTLE_TYPE_SAFARI
  1535. jp z, .canEscape ; jump if it's a safari battle
  1536. ld a, [wLinkState]
  1537. cp LINK_STATE_BATTLING
  1538. jp z, .canEscape
  1539. ld a, [wIsInBattle]
  1540. dec a
  1541. jr nz, .trainerBattle ; jump if it's a trainer battle
  1542. ld a, [wNumRunAttempts]
  1543. inc a
  1544. ld [wNumRunAttempts], a
  1545. ld a, [hli]
  1546. ld [H_MULTIPLICAND + 1], a
  1547. ld a, [hl]
  1548. ld [H_MULTIPLICAND + 2], a
  1549. ld a, [de]
  1550. ld [hEnemySpeed], a
  1551. inc de
  1552. ld a, [de]
  1553. ld [hEnemySpeed + 1], a
  1554. call LoadScreenTilesFromBuffer1
  1555. ld de, H_MULTIPLICAND + 1
  1556. ld hl, hEnemySpeed
  1557. ld c, 2
  1558. call StringCmp
  1559. jr nc, .canEscape ; jump if player speed greater than enemy speed
  1560. xor a
  1561. ld [H_MULTIPLICAND], a
  1562. ld a, 32
  1563. ld [H_MULTIPLIER], a
  1564. call Multiply ; multiply player speed by 32
  1565. ld a, [H_PRODUCT + 2]
  1566. ld [H_DIVIDEND], a
  1567. ld a, [H_PRODUCT + 3]
  1568. ld [H_DIVIDEND + 1], a
  1569. ld a, [hEnemySpeed]
  1570. ld b, a
  1571. ld a, [hEnemySpeed + 1]
  1572. ; divide enemy speed by 4
  1573. srl b
  1574. rr a
  1575. srl b
  1576. rr a
  1577. and a
  1578. jr z, .canEscape ; jump if enemy speed divided by 4, mod 256 is 0
  1579. ld [H_DIVISOR], a ; ((enemy speed / 4) % 256)
  1580. ld b, $2
  1581. call Divide ; divide (player speed * 32) by ((enemy speed / 4) % 256)
  1582. ld a, [H_QUOTIENT + 2]
  1583. and a ; is the quotient greater than 256?
  1584. jr nz, .canEscape ; if so, the player can escape
  1585. ld a, [wNumRunAttempts]
  1586. ld c, a
  1587. ; add 30 to the quotient for each run attempt
  1588. .loop
  1589. dec c
  1590. jr z, .compareWithRandomValue
  1591. ld b, 30
  1592. ld a, [H_QUOTIENT + 3]
  1593. add b
  1594. ld [H_QUOTIENT + 3], a
  1595. jr c, .canEscape
  1596. jr .loop
  1597. .compareWithRandomValue
  1598. call BattleRandom
  1599. ld b, a
  1600. ld a, [H_QUOTIENT + 3]
  1601. cp b
  1602. jr nc, .canEscape ; if the random value was less than or equal to the quotient
  1603. ; plus 30 times the number of attempts, the player can escape
  1604. ; can't escape
  1605. ld a, $1
  1606. ld [wActionResultOrTookBattleTurn], a ; you lose your turn when you can't escape
  1607. ld hl, CantEscapeText
  1608. jr .printCantEscapeOrNoRunningText
  1609. .trainerBattle
  1610. ld hl, NoRunningText
  1611. .printCantEscapeOrNoRunningText
  1612. call PrintText
  1613. ld a, 1
  1614. ld [wForcePlayerToChooseMon], a
  1615. call SaveScreenTilesToBuffer1
  1616. and a ; reset carry
  1617. ret
  1618. .canEscape
  1619. ld a, [wLinkState]
  1620. cp LINK_STATE_BATTLING
  1621. ld a, $2
  1622. jr nz, .playSound
  1623. ; link battle
  1624. call SaveScreenTilesToBuffer1
  1625. xor a
  1626. ld [wActionResultOrTookBattleTurn], a
  1627. ld a, LINKBATTLE_RUN
  1628. ld [wPlayerMoveListIndex], a
  1629. call LinkBattleExchangeData
  1630. call LoadScreenTilesFromBuffer1
  1631. ld a, [wSerialExchangeNybbleReceiveData]
  1632. cp LINKBATTLE_RUN
  1633. ld a, $2
  1634. jr z, .playSound
  1635. dec a
  1636. .playSound
  1637. ld [wBattleResult], a
  1638. ld a, SFX_RUN
  1639. call PlaySoundWaitForCurrent
  1640. ld hl, GotAwayText
  1641. call PrintText
  1642. call WaitForSoundToFinish
  1643. call SaveScreenTilesToBuffer1
  1644. scf ; set carry
  1645. ret
  1646. CantEscapeText:
  1647. TX_FAR _CantEscapeText
  1648. db "@"
  1649. NoRunningText:
  1650. TX_FAR _NoRunningText
  1651. db "@"
  1652. GotAwayText:
  1653. TX_FAR _GotAwayText
  1654. db "@"
  1655. ; copies from party data to battle mon data when sending out a new player mon
  1656. LoadBattleMonFromParty:
  1657. ld a, [wWhichPokemon]
  1658. ld bc, wPartyMon2 - wPartyMon1
  1659. ld hl, wPartyMon1Species
  1660. call AddNTimes
  1661. ld de, wBattleMonSpecies
  1662. ld bc, wBattleMonDVs - wBattleMonSpecies
  1663. call CopyData
  1664. ld bc, wPartyMon1DVs - wPartyMon1OTID
  1665. add hl, bc
  1666. ld de, wBattleMonDVs
  1667. ld bc, NUM_DVS
  1668. call CopyData
  1669. ld de, wBattleMonPP
  1670. ld bc, NUM_MOVES
  1671. call CopyData
  1672. ld de, wBattleMonLevel
  1673. ld bc, wBattleMonPP - wBattleMonLevel
  1674. call CopyData
  1675. ld a, [wBattleMonSpecies2]
  1676. ld [wd0b5], a
  1677. call GetMonHeader
  1678. ld hl, wPartyMonNicks
  1679. ld a, [wPlayerMonNumber]
  1680. call SkipFixedLengthTextEntries
  1681. ld de, wBattleMonNick
  1682. ld bc, NAME_LENGTH
  1683. call CopyData
  1684. ld hl, wBattleMonLevel
  1685. ld de, wPlayerMonUnmodifiedLevel ; block of memory used for unmodified stats
  1686. ld bc, 1 + NUM_STATS * 2
  1687. call CopyData
  1688. call ApplyBurnAndParalysisPenaltiesToPlayer
  1689. call ApplyBadgeStatBoosts
  1690. ld a, $7 ; default stat modifier
  1691. ld b, NUM_STAT_MODS
  1692. ld hl, wPlayerMonAttackMod
  1693. .statModLoop
  1694. ld [hli], a
  1695. dec b
  1696. jr nz, .statModLoop
  1697. ret
  1698. ; copies from enemy party data to current enemy mon data when sending out a new enemy mon
  1699. LoadEnemyMonFromParty:
  1700. ld a, [wWhichPokemon]
  1701. ld bc, wEnemyMon2 - wEnemyMon1
  1702. ld hl, wEnemyMons
  1703. call AddNTimes
  1704. ld de, wEnemyMonSpecies
  1705. ld bc, wEnemyMonDVs - wEnemyMonSpecies
  1706. call CopyData
  1707. ld bc, wEnemyMon1DVs - wEnemyMon1OTID
  1708. add hl, bc
  1709. ld de, wEnemyMonDVs
  1710. ld bc, NUM_DVS
  1711. call CopyData
  1712. ld de, wEnemyMonPP
  1713. ld bc, NUM_MOVES
  1714. call CopyData
  1715. ld de, wEnemyMonLevel
  1716. ld bc, wEnemyMonPP - wEnemyMonLevel
  1717. call CopyData
  1718. ld a, [wEnemyMonSpecies]
  1719. ld [wd0b5], a
  1720. call GetMonHeader
  1721. ld hl, wEnemyMonNicks
  1722. ld a, [wWhichPokemon]
  1723. call SkipFixedLengthTextEntries
  1724. ld de, wEnemyMonNick
  1725. ld bc, NAME_LENGTH
  1726. call CopyData
  1727. ld hl, wEnemyMonLevel
  1728. ld de, wEnemyMonUnmodifiedLevel ; block of memory used for unmodified stats
  1729. ld bc, 1 + NUM_STATS * 2
  1730. call CopyData
  1731. call ApplyBurnAndParalysisPenaltiesToEnemy
  1732. ld hl, wMonHBaseStats
  1733. ld de, wEnemyMonBaseStats
  1734. ld b, NUM_STATS
  1735. .copyBaseStatsLoop
  1736. ld a, [hli]
  1737. ld [de], a
  1738. inc de
  1739. dec b
  1740. jr nz, .copyBaseStatsLoop
  1741. ld a, $7 ; default stat modifier
  1742. ld b, NUM_STAT_MODS
  1743. ld hl, wEnemyMonStatMods
  1744. .statModLoop
  1745. ld [hli], a
  1746. dec b
  1747. jr nz, .statModLoop
  1748. ld a, [wWhichPokemon]
  1749. ld [wEnemyMonPartyPos], a
  1750. ret
  1751. SendOutMon:
  1752. callab PrintSendOutMonMessage
  1753. ld hl, wEnemyMonHP
  1754. ld a, [hli]
  1755. or [hl] ; is enemy mon HP zero?
  1756. jp z, .skipDrawingEnemyHUDAndHPBar; if HP is zero, skip drawing the HUD and HP bar
  1757. call DrawEnemyHUDAndHPBar
  1758. .skipDrawingEnemyHUDAndHPBar
  1759. call DrawPlayerHUDAndHPBar
  1760. predef LoadMonBackPic
  1761. xor a
  1762. ld [hStartTileID], a
  1763. ld hl, wBattleAndStartSavedMenuItem
  1764. ld [hli], a
  1765. ld [hl], a
  1766. ld [wBoostExpByExpAll], a
  1767. ld [wDamageMultipliers], a
  1768. ld [wPlayerMoveNum], a
  1769. ld hl, wPlayerUsedMove
  1770. ld [hli], a
  1771. ld [hl], a
  1772. ld hl, wPlayerStatsToDouble
  1773. ld [hli], a
  1774. ld [hli], a
  1775. ld [hli], a
  1776. ld [hli], a
  1777. ld [hl], a
  1778. ld [wPlayerDisabledMove], a
  1779. ld [wPlayerDisabledMoveNumber], a
  1780. ld [wPlayerMonMinimized], a
  1781. ld b, SET_PAL_BATTLE
  1782. call RunPaletteCommand
  1783. ld hl, wEnemyBattleStatus1
  1784. res USING_TRAPPING_MOVE, [hl]
  1785. ld a, $1
  1786. ld [H_WHOSETURN], a
  1787. ld a, POOF_ANIM
  1788. call PlayMoveAnimation
  1789. coord hl, 4, 11
  1790. predef AnimateSendingOutMon
  1791. ld a, [wcf91]
  1792. call PlayCry
  1793. call PrintEmptyString
  1794. jp SaveScreenTilesToBuffer1
  1795. ; show 2 stages of the player mon getting smaller before disappearing
  1796. AnimateRetreatingPlayerMon:
  1797. coord hl, 1, 5
  1798. lb bc, 7, 7
  1799. call ClearScreenArea
  1800. coord hl, 3, 7
  1801. lb bc, 5, 5
  1802. xor a
  1803. ld [wDownscaledMonSize], a
  1804. ld [hBaseTileID], a
  1805. predef CopyDownscaledMonTiles
  1806. ld c, 4
  1807. call DelayFrames
  1808. call .clearScreenArea
  1809. coord hl, 4, 9
  1810. lb bc, 3, 3
  1811. ld a, 1
  1812. ld [wDownscaledMonSize], a
  1813. xor a
  1814. ld [hBaseTileID], a
  1815. predef CopyDownscaledMonTiles
  1816. call Delay3
  1817. call .clearScreenArea
  1818. ld a, $4c
  1819. Coorda 5, 11
  1820. .clearScreenArea
  1821. coord hl, 1, 5
  1822. lb bc, 7, 7
  1823. jp ClearScreenArea
  1824. ; reads player's current mon's HP into wBattleMonHP
  1825. ReadPlayerMonCurHPAndStatus:
  1826. ld a, [wPlayerMonNumber]
  1827. ld hl, wPartyMon1HP
  1828. ld bc, wPartyMon2 - wPartyMon1
  1829. call AddNTimes
  1830. ld d, h
  1831. ld e, l
  1832. ld hl, wBattleMonHP
  1833. ld bc, $4 ; 2 bytes HP, 1 byte unknown (unused?), 1 byte status
  1834. jp CopyData
  1835. DrawHUDsAndHPBars:
  1836. call DrawPlayerHUDAndHPBar
  1837. jp DrawEnemyHUDAndHPBar
  1838. DrawPlayerHUDAndHPBar:
  1839. xor a
  1840. ld [H_AUTOBGTRANSFERENABLED], a
  1841. coord hl, 9, 7
  1842. lb bc, 5, 11
  1843. call ClearScreenArea
  1844. callab PlacePlayerHUDTiles
  1845. coord hl, 18, 9
  1846. ld [hl], $73
  1847. ld de, wBattleMonNick
  1848. coord hl, 10, 7
  1849. call CenterMonName
  1850. call PlaceString
  1851. ld hl, wBattleMonSpecies
  1852. ld de, wLoadedMon
  1853. ld bc, wBattleMonDVs - wBattleMonSpecies
  1854. call CopyData
  1855. ld hl, wBattleMonLevel
  1856. ld de, wLoadedMonLevel
  1857. ld bc, wBattleMonPP - wBattleMonLevel
  1858. call CopyData
  1859. coord hl, 14, 8
  1860. push hl
  1861. inc hl
  1862. ld de, wLoadedMonStatus
  1863. call PrintStatusConditionNotFainted
  1864. pop hl
  1865. jr nz, .doNotPrintLevel
  1866. call PrintLevel
  1867. .doNotPrintLevel
  1868. ld a, [wLoadedMonSpecies]
  1869. ld [wcf91], a
  1870. coord hl, 10, 9
  1871. predef DrawHP
  1872. ld a, $1
  1873. ld [H_AUTOBGTRANSFERENABLED], a
  1874. ld hl, wPlayerHPBarColor
  1875. call GetBattleHealthBarColor
  1876. ld hl, wBattleMonHP
  1877. ld a, [hli]
  1878. or [hl]
  1879. jr z, .fainted
  1880. ld a, [wLowHealthAlarmDisabled]
  1881. and a ; has the alarm been disabled because the player has already won?
  1882. ret nz ; if so, return
  1883. ld a, [wPlayerHPBarColor]
  1884. cp HP_BAR_RED
  1885. jr z, .setLowHealthAlarm
  1886. .fainted
  1887. ld hl, wLowHealthAlarm
  1888. bit 7, [hl] ;low health alarm enabled?
  1889. ld [hl], $0
  1890. ret z
  1891. xor a
  1892. ld [wChannelSoundIDs + Ch4], a
  1893. ret
  1894. .setLowHealthAlarm
  1895. ld hl, wLowHealthAlarm
  1896. set 7, [hl] ;enable low health alarm
  1897. ret
  1898. DrawEnemyHUDAndHPBar:
  1899. xor a
  1900. ld [H_AUTOBGTRANSFERENABLED], a
  1901. coord hl, 0, 0
  1902. lb bc, 4, 12
  1903. call ClearScreenArea
  1904. callab PlaceEnemyHUDTiles
  1905. ld de, wEnemyMonNick
  1906. coord hl, 1, 0
  1907. call CenterMonName
  1908. call PlaceString
  1909. coord hl, 4, 1
  1910. push hl
  1911. inc hl
  1912. ld de, wEnemyMonStatus
  1913. call PrintStatusConditionNotFainted
  1914. pop hl
  1915. jr nz, .skipPrintLevel ; if the mon has a status condition, skip printing the level
  1916. ld a, [wEnemyMonLevel]
  1917. ld [wLoadedMonLevel], a
  1918. call PrintLevel
  1919. .skipPrintLevel
  1920. ld hl, wEnemyMonHP
  1921. ld a, [hli]
  1922. ld [H_MULTIPLICAND + 1], a
  1923. ld a, [hld]
  1924. ld [H_MULTIPLICAND + 2], a
  1925. or [hl] ; is current HP zero?
  1926. jr nz, .hpNonzero
  1927. ; current HP is 0
  1928. ; set variables for DrawHPBar
  1929. ld c, a
  1930. ld e, a
  1931. ld d, $6
  1932. jp .drawHPBar
  1933. .hpNonzero
  1934. xor a
  1935. ld [H_MULTIPLICAND], a
  1936. ld a, 48
  1937. ld [H_MULTIPLIER], a
  1938. call Multiply ; multiply current HP by 48
  1939. ld hl, wEnemyMonMaxHP
  1940. ld a, [hli]
  1941. ld b, a
  1942. ld a, [hl]
  1943. ld [H_DIVISOR], a
  1944. ld a, b
  1945. and a ; is max HP > 255?
  1946. jr z, .doDivide
  1947. ; if max HP > 255, scale both (current HP * 48) and max HP by dividing by 4 so that max HP fits in one byte
  1948. ; (it needs to be one byte so it can be used as the divisor for the Divide function)
  1949. ld a, [H_DIVISOR]
  1950. srl b
  1951. rr a
  1952. srl b
  1953. rr a
  1954. ld [H_DIVISOR], a
  1955. ld a, [H_PRODUCT + 2]
  1956. ld b, a
  1957. srl b
  1958. ld a, [H_PRODUCT + 3]
  1959. rr a
  1960. srl b
  1961. rr a
  1962. ld [H_PRODUCT + 3], a
  1963. ld a, b
  1964. ld [H_PRODUCT + 2], a
  1965. .doDivide
  1966. ld a, [H_PRODUCT + 2]
  1967. ld [H_DIVIDEND], a
  1968. ld a, [H_PRODUCT + 3]
  1969. ld [H_DIVIDEND + 1], a
  1970. ld a, $2
  1971. ld b, a
  1972. call Divide ; divide (current HP * 48) by max HP
  1973. ld a, [H_QUOTIENT + 3]
  1974. ; set variables for DrawHPBar
  1975. ld e, a
  1976. ld a, $6
  1977. ld d, a
  1978. ld c, a
  1979. .drawHPBar
  1980. xor a
  1981. ld [wHPBarType], a
  1982. coord hl, 2, 2
  1983. call DrawHPBar
  1984. ld a, $1
  1985. ld [H_AUTOBGTRANSFERENABLED], a
  1986. ld hl, wEnemyHPBarColor
  1987. GetBattleHealthBarColor:
  1988. ld b, [hl]
  1989. call GetHealthBarColor
  1990. ld a, [hl]
  1991. cp b
  1992. ret z
  1993. ld b, SET_PAL_BATTLE
  1994. jp RunPaletteCommand
  1995. ; center's mon's name on the battle screen
  1996. ; if the name is 1 or 2 letters long, it is printed 2 spaces more to the right than usual
  1997. ; (i.e. for names longer than 4 letters)
  1998. ; if the name is 3 or 4 letters long, it is printed 1 space more to the right than usual
  1999. ; (i.e. for names longer than 4 letters)
  2000. CenterMonName:
  2001. push de
  2002. inc hl
  2003. inc hl
  2004. ld b, $2
  2005. .loop
  2006. inc de
  2007. ld a, [de]
  2008. cp "@"
  2009. jr z, .done
  2010. inc de
  2011. ld a, [de]
  2012. cp "@"
  2013. jr z, .done
  2014. dec hl
  2015. dec b
  2016. jr nz, .loop
  2017. .done
  2018. pop de
  2019. ret
  2020. DisplayBattleMenu:
  2021. call LoadScreenTilesFromBuffer1 ; restore saved screen
  2022. ld a, [wBattleType]
  2023. and a
  2024. jr nz, .nonstandardbattle
  2025. call DrawHUDsAndHPBars
  2026. call PrintEmptyString
  2027. call SaveScreenTilesToBuffer1
  2028. .nonstandardbattle
  2029. ld a, [wBattleType]
  2030. cp BATTLE_TYPE_SAFARI
  2031. ld a, BATTLE_MENU_TEMPLATE
  2032. jr nz, .menuselected
  2033. ld a, SAFARI_BATTLE_MENU_TEMPLATE
  2034. .menuselected
  2035. ld [wTextBoxID], a
  2036. call DisplayTextBoxID
  2037. ld a, [wBattleType]
  2038. dec a
  2039. jp nz, .handleBattleMenuInput ; handle menu input if it's not the old man tutorial
  2040. ; the following happens for the old man tutorial
  2041. ld hl, wPlayerName
  2042. ld de, wGrassRate
  2043. ld bc, NAME_LENGTH
  2044. call CopyData ; temporarily save the player name in unused space,
  2045. ; which is supposed to get overwritten when entering a
  2046. ; map with wild Pokémon. Due to an oversight, the data
  2047. ; may not get overwritten (cinnabar) and the infamous
  2048. ; Missingno. glitch can show up.
  2049. ld hl, .oldManName
  2050. ld de, wPlayerName
  2051. ld bc, NAME_LENGTH
  2052. call CopyData
  2053. ; the following simulates the keystrokes by drawing menus on screen
  2054. coord hl, 9, 14
  2055. ld [hl], "▶"
  2056. ld c, 80
  2057. call DelayFrames
  2058. ld [hl], " "
  2059. coord hl, 9, 16
  2060. ld [hl], "▶"
  2061. ld c, 50
  2062. call DelayFrames
  2063. ld [hl], "▷"
  2064. ld a, $2 ; select the "ITEM" menu
  2065. jp .upperLeftMenuItemWasNotSelected
  2066. .oldManName
  2067. db "OLD MAN@"
  2068. .handleBattleMenuInput
  2069. ld a, [wBattleAndStartSavedMenuItem]
  2070. ld [wCurrentMenuItem], a
  2071. ld [wLastMenuItem], a
  2072. sub 2 ; check if the cursor is in the left column
  2073. jr c, .leftColumn
  2074. ; cursor is in the right column
  2075. ld [wCurrentMenuItem], a
  2076. ld [wLastMenuItem], a
  2077. jr .rightColumn
  2078. .leftColumn ; put cursor in left column of menu
  2079. ld a, [wBattleType]
  2080. cp BATTLE_TYPE_SAFARI
  2081. ld a, " "
  2082. jr z, .safariLeftColumn
  2083. ; put cursor in left column for normal battle menu (i.e. when it's not a Safari battle)
  2084. Coorda 15, 14 ; clear upper cursor position in right column
  2085. Coorda 15, 16 ; clear lower cursor position in right column
  2086. ld b, $9 ; top menu item X
  2087. jr .leftColumn_WaitForInput
  2088. .safariLeftColumn
  2089. Coorda 13, 14
  2090. Coorda 13, 16
  2091. coord hl, 7, 14
  2092. ld de, wNumSafariBalls
  2093. lb bc, 1, 2
  2094. call PrintNumber
  2095. ld b, $1 ; top menu item X
  2096. .leftColumn_WaitForInput
  2097. ld hl, wTopMenuItemY
  2098. ld a, $e
  2099. ld [hli], a ; wTopMenuItemY
  2100. ld a, b
  2101. ld [hli], a ; wTopMenuItemX
  2102. inc hl
  2103. inc hl
  2104. ld a, $1
  2105. ld [hli], a ; wMaxMenuItem
  2106. ld [hl], D_RIGHT | A_BUTTON ; wMenuWatchedKeys
  2107. call HandleMenuInput
  2108. bit 4, a ; check if right was pressed
  2109. jr nz, .rightColumn
  2110. jr .AButtonPressed ; the A button was pressed
  2111. .rightColumn ; put cursor in right column of menu
  2112. ld a, [wBattleType]
  2113. cp BATTLE_TYPE_SAFARI
  2114. ld a, " "
  2115. jr z, .safariRightColumn
  2116. ; put cursor in right column for normal battle menu (i.e. when it's not a Safari battle)
  2117. Coorda 9, 14 ; clear upper cursor position in left column
  2118. Coorda 9, 16 ; clear lower cursor position in left column
  2119. ld b, $f ; top menu item X
  2120. jr .rightColumn_WaitForInput
  2121. .safariRightColumn
  2122. Coorda 1, 14 ; clear upper cursor position in left column
  2123. Coorda 1, 16 ; clear lower cursor position in left column
  2124. coord hl, 7, 14
  2125. ld de, wNumSafariBalls
  2126. lb bc, 1, 2
  2127. call PrintNumber
  2128. ld b, $d ; top menu item X
  2129. .rightColumn_WaitForInput
  2130. ld hl, wTopMenuItemY
  2131. ld a, $e
  2132. ld [hli], a ; wTopMenuItemY
  2133. ld a, b
  2134. ld [hli], a ; wTopMenuItemX
  2135. inc hl
  2136. inc hl
  2137. ld a, $1
  2138. ld [hli], a ; wMaxMenuItem
  2139. ld a, D_LEFT | A_BUTTON
  2140. ld [hli], a ; wMenuWatchedKeys
  2141. call HandleMenuInput
  2142. bit 5, a ; check if left was pressed
  2143. jr nz, .leftColumn ; if left was pressed, jump
  2144. ld a, [wCurrentMenuItem]
  2145. add $2 ; if we're in the right column, the actual id is +2
  2146. ld [wCurrentMenuItem], a
  2147. .AButtonPressed
  2148. call PlaceUnfilledArrowMenuCursor
  2149. ld a, [wBattleType]
  2150. cp BATTLE_TYPE_SAFARI
  2151. ld a, [wCurrentMenuItem]
  2152. ld [wBattleAndStartSavedMenuItem], a
  2153. jr z, .handleMenuSelection
  2154. ; not Safari battle
  2155. ; swap the IDs of the item menu and party menu (this is probably because they swapped the positions
  2156. ; of these menu items in first generation English versions)
  2157. cp $1 ; was the item menu selected?
  2158. jr nz, .notItemMenu
  2159. ; item menu was selected
  2160. inc a ; increment a to 2
  2161. jr .handleMenuSelection
  2162. .notItemMenu
  2163. cp $2 ; was the party menu selected?
  2164. jr nz, .handleMenuSelection
  2165. ; party menu selected
  2166. dec a ; decrement a to 1
  2167. .handleMenuSelection
  2168. and a
  2169. jr nz, .upperLeftMenuItemWasNotSelected
  2170. ; the upper left menu item was selected
  2171. ld a, [wBattleType]
  2172. cp BATTLE_TYPE_SAFARI
  2173. jr z, .throwSafariBallWasSelected
  2174. ; the "FIGHT" menu was selected
  2175. xor a
  2176. ld [wNumRunAttempts], a
  2177. jp LoadScreenTilesFromBuffer1 ; restore saved screen and return
  2178. .throwSafariBallWasSelected
  2179. ld a, SAFARI_BALL
  2180. ld [wcf91], a
  2181. jr UseBagItem
  2182. .upperLeftMenuItemWasNotSelected ; a menu item other than the upper left item was selected
  2183. cp $2
  2184. jp nz, PartyMenuOrRockOrRun
  2185. ; either the bag (normal battle) or bait (safari battle) was selected
  2186. ld a, [wLinkState]
  2187. cp LINK_STATE_BATTLING
  2188. jr nz, .notLinkBattle
  2189. ; can't use items in link battles
  2190. ld hl, ItemsCantBeUsedHereText
  2191. call PrintText
  2192. jp DisplayBattleMenu
  2193. .notLinkBattle
  2194. call SaveScreenTilesToBuffer2
  2195. ld a, [wBattleType]
  2196. cp BATTLE_TYPE_SAFARI
  2197. jr nz, BagWasSelected
  2198. ; bait was selected
  2199. ld a, SAFARI_BAIT
  2200. ld [wcf91], a
  2201. jr UseBagItem
  2202. BagWasSelected:
  2203. call LoadScreenTilesFromBuffer1
  2204. ld a, [wBattleType]
  2205. and a ; is it a normal battle?
  2206. jr nz, .next
  2207. ; normal battle
  2208. call DrawHUDsAndHPBars
  2209. .next
  2210. ld a, [wBattleType]
  2211. dec a ; is it the old man tutorial?
  2212. jr nz, DisplayPlayerBag ; no, it is a normal battle
  2213. ld hl, OldManItemList
  2214. ld a, l
  2215. ld [wListPointer], a
  2216. ld a, h
  2217. ld [wListPointer + 1], a
  2218. jr DisplayBagMenu
  2219. OldManItemList:
  2220. db 1 ; # items
  2221. db POKE_BALL, 50
  2222. db -1
  2223. DisplayPlayerBag:
  2224. ; get the pointer to player's bag when in a normal battle
  2225. ld hl, wNumBagItems
  2226. ld a, l
  2227. ld [wListPointer], a
  2228. ld a, h
  2229. ld [wListPointer + 1], a
  2230. DisplayBagMenu:
  2231. xor a
  2232. ld [wPrintItemPrices], a
  2233. ld a, ITEMLISTMENU
  2234. ld [wListMenuID], a
  2235. ld a, [wBagSavedMenuItem]
  2236. ld [wCurrentMenuItem], a
  2237. call DisplayListMenuID
  2238. ld a, [wCurrentMenuItem]
  2239. ld [wBagSavedMenuItem], a
  2240. ld a, $0
  2241. ld [wMenuWatchMovingOutOfBounds], a
  2242. ld [wMenuItemToSwap], a
  2243. jp c, DisplayBattleMenu ; go back to battle menu if an item was not selected
  2244. UseBagItem:
  2245. ; either use an item from the bag or use a safari zone item
  2246. ld a, [wcf91]
  2247. ld [wd11e], a
  2248. call GetItemName
  2249. call CopyStringToCF4B ; copy name
  2250. xor a
  2251. ld [wPseudoItemID], a
  2252. call UseItem
  2253. call LoadHudTilePatterns
  2254. call ClearSprites
  2255. xor a
  2256. ld [wCurrentMenuItem], a
  2257. ld a, [wBattleType]
  2258. cp BATTLE_TYPE_SAFARI
  2259. jr z, .checkIfMonCaptured
  2260. ld a, [wActionResultOrTookBattleTurn]
  2261. and a ; was the item used successfully?
  2262. jp z, BagWasSelected ; if not, go back to the bag menu
  2263. ld a, [wPlayerBattleStatus1]
  2264. bit USING_TRAPPING_MOVE, a ; is the player using a multi-turn move like wrap?
  2265. jr z, .checkIfMonCaptured
  2266. ld hl, wPlayerNumAttacksLeft
  2267. dec [hl]
  2268. jr nz, .checkIfMonCaptured
  2269. ld hl, wPlayerBattleStatus1
  2270. res USING_TRAPPING_MOVE, [hl] ; not using multi-turn move any more
  2271. .checkIfMonCaptured
  2272. ld a, [wCapturedMonSpecies]
  2273. and a ; was the enemy mon captured with a ball?
  2274. jr nz, .returnAfterCapturingMon
  2275. ld a, [wBattleType]
  2276. cp BATTLE_TYPE_SAFARI
  2277. jr z, .returnAfterUsingItem_NoCapture
  2278. ; not a safari battle
  2279. call LoadScreenTilesFromBuffer1
  2280. call DrawHUDsAndHPBars
  2281. call Delay3
  2282. .returnAfterUsingItem_NoCapture
  2283. call GBPalNormal
  2284. and a ; reset carry
  2285. ret
  2286. .returnAfterCapturingMon
  2287. call GBPalNormal
  2288. xor a
  2289. ld [wCapturedMonSpecies], a
  2290. ld a, $2
  2291. ld [wBattleResult], a
  2292. scf ; set carry
  2293. ret
  2294. ItemsCantBeUsedHereText:
  2295. TX_FAR _ItemsCantBeUsedHereText
  2296. db "@"
  2297. PartyMenuOrRockOrRun:
  2298. dec a ; was Run selected?
  2299. jp nz, BattleMenu_RunWasSelected
  2300. ; party menu or rock was selected
  2301. call SaveScreenTilesToBuffer2
  2302. ld a, [wBattleType]
  2303. cp BATTLE_TYPE_SAFARI
  2304. jr nz, .partyMenuWasSelected
  2305. ; safari battle
  2306. ld a, SAFARI_ROCK
  2307. ld [wcf91], a
  2308. jp UseBagItem
  2309. .partyMenuWasSelected
  2310. call LoadScreenTilesFromBuffer1
  2311. xor a ; NORMAL_PARTY_MENU
  2312. ld [wPartyMenuTypeOrMessageID], a
  2313. ld [wMenuItemToSwap], a
  2314. call DisplayPartyMenu
  2315. .checkIfPartyMonWasSelected
  2316. jp nc, .partyMonWasSelected ; if a party mon was selected, jump, else we quit the party menu
  2317. .quitPartyMenu
  2318. call ClearSprites
  2319. call GBPalWhiteOut
  2320. call LoadHudTilePatterns
  2321. call LoadScreenTilesFromBuffer2
  2322. call RunDefaultPaletteCommand
  2323. call GBPalNormal
  2324. jp DisplayBattleMenu
  2325. .partyMonDeselected
  2326. coord hl, 11, 11
  2327. ld bc, 6 * SCREEN_WIDTH + 9
  2328. ld a, " "
  2329. call FillMemory
  2330. xor a ; NORMAL_PARTY_MENU
  2331. ld [wPartyMenuTypeOrMessageID], a
  2332. call GoBackToPartyMenu
  2333. jr .checkIfPartyMonWasSelected
  2334. .partyMonWasSelected
  2335. ld a, SWITCH_STATS_CANCEL_MENU_TEMPLATE
  2336. ld [wTextBoxID], a
  2337. call DisplayTextBoxID
  2338. ld hl, wTopMenuItemY
  2339. ld a, $c
  2340. ld [hli], a ; wTopMenuItemY
  2341. ld [hli], a ; wTopMenuItemX
  2342. xor a
  2343. ld [hli], a ; wCurrentMenuItem
  2344. inc hl
  2345. ld a, $2
  2346. ld [hli], a ; wMaxMenuItem
  2347. ld a, B_BUTTON | A_BUTTON
  2348. ld [hli], a ; wMenuWatchedKeys
  2349. xor a
  2350. ld [hl], a ; wLastMenuItem
  2351. call HandleMenuInput
  2352. bit 1, a ; was A pressed?
  2353. jr nz, .partyMonDeselected ; if B was pressed, jump
  2354. ; A was pressed
  2355. call PlaceUnfilledArrowMenuCursor
  2356. ld a, [wCurrentMenuItem]
  2357. cp $2 ; was Cancel selected?
  2358. jr z, .quitPartyMenu ; if so, quit the party menu entirely
  2359. and a ; was Switch selected?
  2360. jr z, .switchMon ; if so, jump
  2361. ; Stats was selected
  2362. xor a ; PLAYER_PARTY_DATA
  2363. ld [wMonDataLocation], a
  2364. ld hl, wPartyMon1
  2365. call ClearSprites
  2366. ; display the two status screens
  2367. predef StatusScreen
  2368. predef StatusScreen2
  2369. ; now we need to reload the enemy mon pic
  2370. ld a, [wEnemyBattleStatus2]
  2371. bit HAS_SUBSTITUTE_UP, a ; does the enemy mon have a substitute?
  2372. ld hl, AnimationSubstitute
  2373. jr nz, .doEnemyMonAnimation
  2374. ; enemy mon doesn't have substitute
  2375. ld a, [wEnemyMonMinimized]
  2376. and a ; has the enemy mon used Minimise?
  2377. ld hl, AnimationMinimizeMon
  2378. jr nz, .doEnemyMonAnimation
  2379. ; enemy mon is not minimised
  2380. ld a, [wEnemyMonSpecies]
  2381. ld [wcf91], a
  2382. ld [wd0b5], a
  2383. call GetMonHeader
  2384. ld de, vFrontPic
  2385. call LoadMonFrontSprite
  2386. jr .enemyMonPicReloaded
  2387. .doEnemyMonAnimation
  2388. ld b, BANK(AnimationSubstitute) ; BANK(AnimationMinimizeMon)
  2389. call Bankswitch
  2390. .enemyMonPicReloaded ; enemy mon pic has been reloaded, so return to the party menu
  2391. jp .partyMenuWasSelected
  2392. .switchMon
  2393. ld a, [wPlayerMonNumber]
  2394. ld d, a
  2395. ld a, [wWhichPokemon]
  2396. cp d ; check if the mon to switch to is already out
  2397. jr nz, .notAlreadyOut
  2398. ; mon is already out
  2399. ld hl, AlreadyOutText
  2400. call PrintText
  2401. jp .partyMonDeselected
  2402. .notAlreadyOut
  2403. call HasMonFainted
  2404. jp z, .partyMonDeselected ; can't switch to fainted mon
  2405. ld a, $1
  2406. ld [wActionResultOrTookBattleTurn], a
  2407. call GBPalWhiteOut
  2408. call ClearSprites
  2409. call LoadHudTilePatterns
  2410. call LoadScreenTilesFromBuffer1
  2411. call RunDefaultPaletteCommand
  2412. call GBPalNormal
  2413. ; fall through to SwitchPlayerMon
  2414. SwitchPlayerMon:
  2415. callab RetreatMon
  2416. ld c, 50
  2417. call DelayFrames
  2418. call AnimateRetreatingPlayerMon
  2419. ld a, [wWhichPokemon]
  2420. ld [wPlayerMonNumber], a
  2421. ld c, a
  2422. ld b, FLAG_SET
  2423. push bc
  2424. ld hl, wPartyGainExpFlags
  2425. predef FlagActionPredef
  2426. pop bc
  2427. ld hl, wPartyFoughtCurrentEnemyFlags
  2428. predef FlagActionPredef
  2429. call LoadBattleMonFromParty
  2430. call SendOutMon
  2431. call SaveScreenTilesToBuffer1
  2432. ld a, $2
  2433. ld [wCurrentMenuItem], a
  2434. and a
  2435. ret
  2436. AlreadyOutText:
  2437. TX_FAR _AlreadyOutText
  2438. db "@"
  2439. BattleMenu_RunWasSelected:
  2440. call LoadScreenTilesFromBuffer1
  2441. ld a, $3
  2442. ld [wCurrentMenuItem], a
  2443. ld hl, wBattleMonSpeed
  2444. ld de, wEnemyMonSpeed
  2445. call TryRunningFromBattle
  2446. ld a, 0
  2447. ld [wForcePlayerToChooseMon], a
  2448. ret c
  2449. ld a, [wActionResultOrTookBattleTurn]
  2450. and a
  2451. ret nz ; return if the player couldn't escape
  2452. jp DisplayBattleMenu
  2453. MoveSelectionMenu:
  2454. ld a, [wMoveMenuType]
  2455. dec a
  2456. jr z, .mimicmenu
  2457. dec a
  2458. jr z, .relearnmenu
  2459. jr .regularmenu
  2460. .loadmoves
  2461. ld de, wMoves
  2462. ld bc, NUM_MOVES
  2463. call CopyData
  2464. callab FormatMovesString
  2465. ret
  2466. .writemoves
  2467. ld de, wMovesString
  2468. ld a, [hFlags_0xFFF6]
  2469. set 2, a
  2470. ld [hFlags_0xFFF6], a
  2471. call PlaceString
  2472. ld a, [hFlags_0xFFF6]
  2473. res 2, a
  2474. ld [hFlags_0xFFF6], a
  2475. ret
  2476. .regularmenu
  2477. call AnyMoveToSelect
  2478. ret z
  2479. ld hl, wBattleMonMoves
  2480. call .loadmoves
  2481. coord hl, 4, 12
  2482. ld b, 4
  2483. ld c, 14
  2484. di ; out of pure coincidence, it is possible for vblank to occur between the di and ei
  2485. ; so it is necessary to put the di ei block to not cause tearing
  2486. call TextBoxBorder
  2487. coord hl, 4, 12
  2488. ld [hl], $7a
  2489. coord hl, 10, 12
  2490. ld [hl], $7e
  2491. ei
  2492. coord hl, 6, 13
  2493. call .writemoves
  2494. ld b, $5
  2495. ld a, $c
  2496. jr .menuset
  2497. .mimicmenu
  2498. ld hl, wEnemyMonMoves
  2499. call .loadmoves
  2500. coord hl, 0, 7
  2501. ld b, 4
  2502. ld c, 14
  2503. call TextBoxBorder
  2504. coord hl, 2, 8
  2505. call .writemoves
  2506. ld b, $1
  2507. ld a, $7
  2508. jr .menuset
  2509. .relearnmenu
  2510. ld a, [wWhichPokemon]
  2511. ld hl, wPartyMon1Moves
  2512. ld bc, wPartyMon2 - wPartyMon1
  2513. call AddNTimes
  2514. call .loadmoves
  2515. coord hl, 4, 7
  2516. ld b, 4
  2517. ld c, 14
  2518. call TextBoxBorder
  2519. coord hl, 6, 8
  2520. call .writemoves
  2521. ld b, $5
  2522. ld a, $7
  2523. .menuset
  2524. ld hl, wTopMenuItemY
  2525. ld [hli], a ; wTopMenuItemY
  2526. ld a, b
  2527. ld [hli], a ; wTopMenuItemX
  2528. ld a, [wMoveMenuType]
  2529. cp $1
  2530. jr z, .selectedmoveknown
  2531. ld a, $1
  2532. jr nc, .selectedmoveknown
  2533. ld a, [wPlayerMoveListIndex]
  2534. inc a
  2535. .selectedmoveknown
  2536. ld [hli], a ; wCurrentMenuItem
  2537. inc hl ; wTileBehindCursor untouched
  2538. ld a, [wNumMovesMinusOne]
  2539. inc a
  2540. inc a
  2541. ld [hli], a ; wMaxMenuItem
  2542. ld a, [wMoveMenuType]
  2543. dec a
  2544. ld b, D_UP | D_DOWN | A_BUTTON
  2545. jr z, .matchedkeyspicked
  2546. dec a
  2547. ld b, D_UP | D_DOWN | A_BUTTON | B_BUTTON
  2548. jr z, .matchedkeyspicked
  2549. ld a, [wLinkState]
  2550. cp LINK_STATE_BATTLING
  2551. jr z, .matchedkeyspicked
  2552. ld a, [wFlags_D733]
  2553. bit BIT_TEST_BATTLE, a
  2554. ld b, D_UP | D_DOWN | A_BUTTON | B_BUTTON | SELECT
  2555. jr z, .matchedkeyspicked
  2556. ld b, $ff
  2557. .matchedkeyspicked
  2558. ld a, b
  2559. ld [hli], a ; wMenuWatchedKeys
  2560. ld a, [wMoveMenuType]
  2561. cp $1
  2562. jr z, .movelistindex1
  2563. ld a, [wPlayerMoveListIndex]
  2564. inc a
  2565. .movelistindex1
  2566. ld [hl], a
  2567. ; fallthrough
  2568. SelectMenuItem:
  2569. ld a, [wMoveMenuType]
  2570. and a
  2571. jr z, .battleselect
  2572. dec a
  2573. jr nz, .select
  2574. coord hl, 1, 14
  2575. ld de, WhichTechniqueString
  2576. call PlaceString
  2577. jr .select
  2578. .battleselect
  2579. ld a, [wFlags_D733]
  2580. bit BIT_TEST_BATTLE, a
  2581. jr nz, .select
  2582. call PrintMenuItem
  2583. ld a, [wMenuItemToSwap]
  2584. and a
  2585. jr z, .select
  2586. coord hl, 5, 13
  2587. dec a
  2588. ld bc, SCREEN_WIDTH
  2589. call AddNTimes
  2590. ld [hl], "▷"
  2591. .select
  2592. ld hl, hFlags_0xFFF6
  2593. set 1, [hl]
  2594. call HandleMenuInput
  2595. ld hl, hFlags_0xFFF6
  2596. res 1, [hl]
  2597. bit 6, a
  2598. jp nz, SelectMenuItem_CursorUp ; up
  2599. bit 7, a
  2600. jp nz, SelectMenuItem_CursorDown ; down
  2601. bit 2, a
  2602. jp nz, SwapMovesInMenu ; select
  2603. bit 1, a ; B, but was it reset above?
  2604. push af
  2605. xor a
  2606. ld [wMenuItemToSwap], a
  2607. ld a, [wCurrentMenuItem]
  2608. dec a
  2609. ld [wCurrentMenuItem], a
  2610. ld b, a
  2611. ld a, [wMoveMenuType]
  2612. dec a ; if not mimic
  2613. jr nz, .notB
  2614. pop af
  2615. ret
  2616. .notB
  2617. dec a
  2618. ld a, b
  2619. ld [wPlayerMoveListIndex], a
  2620. jr nz, .moveselected
  2621. pop af
  2622. ret
  2623. .moveselected
  2624. pop af
  2625. ret nz
  2626. ld hl, wBattleMonPP
  2627. ld a, [wCurrentMenuItem]
  2628. ld c, a
  2629. ld b, $0
  2630. add hl, bc
  2631. ld a, [hl]
  2632. and $3f
  2633. jr z, .noPP
  2634. ld a, [wPlayerDisabledMove]
  2635. swap a
  2636. and $f
  2637. dec a
  2638. cp c
  2639. jr z, .disabled
  2640. ld a, [wPlayerBattleStatus3]
  2641. bit 3, a ; transformed
  2642. jr nz, .dummy ; game freak derp
  2643. .dummy
  2644. ld a, [wCurrentMenuItem]
  2645. ld hl, wBattleMonMoves
  2646. ld c, a
  2647. ld b, $0
  2648. add hl, bc
  2649. ld a, [hl]
  2650. ld [wPlayerSelectedMove], a
  2651. xor a
  2652. ret
  2653. .disabled
  2654. ld hl, MoveDisabledText
  2655. jr .print
  2656. .noPP
  2657. ld hl, MoveNoPPText
  2658. .print
  2659. call PrintText
  2660. call LoadScreenTilesFromBuffer1
  2661. jp MoveSelectionMenu
  2662. MoveNoPPText:
  2663. TX_FAR _MoveNoPPText
  2664. db "@"
  2665. MoveDisabledText:
  2666. TX_FAR _MoveDisabledText
  2667. db "@"
  2668. WhichTechniqueString:
  2669. db "WHICH TECHNIQUE?@"
  2670. SelectMenuItem_CursorUp:
  2671. ld a, [wCurrentMenuItem]
  2672. and a
  2673. jp nz, SelectMenuItem
  2674. call EraseMenuCursor
  2675. ld a, [wNumMovesMinusOne]
  2676. inc a
  2677. ld [wCurrentMenuItem], a
  2678. jp SelectMenuItem
  2679. SelectMenuItem_CursorDown:
  2680. ld a, [wCurrentMenuItem]
  2681. ld b, a
  2682. ld a, [wNumMovesMinusOne]
  2683. inc a
  2684. inc a
  2685. cp b
  2686. jp nz, SelectMenuItem
  2687. call EraseMenuCursor
  2688. ld a, $1
  2689. ld [wCurrentMenuItem], a
  2690. jp SelectMenuItem
  2691. AnyMoveToSelect:
  2692. ; return z and Struggle as the selected move if all moves have 0 PP and/or are disabled
  2693. ld a, STRUGGLE
  2694. ld [wPlayerSelectedMove], a
  2695. ld a, [wPlayerDisabledMove]
  2696. and a
  2697. ld hl, wBattleMonPP
  2698. jr nz, .handleDisabledMove
  2699. ld a, [hli]
  2700. or [hl]
  2701. inc hl
  2702. or [hl]
  2703. inc hl
  2704. or [hl]
  2705. and $3f
  2706. ret nz
  2707. jr .noMovesLeft
  2708. .handleDisabledMove
  2709. swap a
  2710. and $f ; get disabled move
  2711. ld b, a
  2712. ld d, NUM_MOVES + 1
  2713. xor a
  2714. .handleDisabledMovePPLoop
  2715. dec d
  2716. jr z, .allMovesChecked
  2717. ld c, [hl] ; get move PP
  2718. inc hl
  2719. dec b ; is this the disabled move?
  2720. jr z, .handleDisabledMovePPLoop ; if so, ignore its PP value
  2721. or c
  2722. jr .handleDisabledMovePPLoop
  2723. .allMovesChecked
  2724. and a ; any PP left?
  2725. ret nz ; return if a move has PP left
  2726. .noMovesLeft
  2727. ld hl, NoMovesLeftText
  2728. call PrintText
  2729. ld c, 60
  2730. call DelayFrames
  2731. xor a
  2732. ret
  2733. NoMovesLeftText:
  2734. TX_FAR _NoMovesLeftText
  2735. db "@"
  2736. SwapMovesInMenu:
  2737. ld a, [wMenuItemToSwap]
  2738. and a
  2739. jr z, .noMenuItemSelected
  2740. ld hl, wBattleMonMoves
  2741. call .swapBytes ; swap moves
  2742. ld hl, wBattleMonPP
  2743. call .swapBytes ; swap move PP
  2744. ; update the index of the disabled move if necessary
  2745. ld hl, wPlayerDisabledMove
  2746. ld a, [hl]
  2747. swap a
  2748. and $f
  2749. ld b, a
  2750. ld a, [wCurrentMenuItem]
  2751. cp b
  2752. jr nz, .next
  2753. ld a, [hl]
  2754. and $f
  2755. ld b, a
  2756. ld a, [wMenuItemToSwap]
  2757. swap a
  2758. add b
  2759. ld [hl], a
  2760. jr .swapMovesInPartyMon
  2761. .next
  2762. ld a, [wMenuItemToSwap]
  2763. cp b
  2764. jr nz, .swapMovesInPartyMon
  2765. ld a, [hl]
  2766. and $f
  2767. ld b, a
  2768. ld a, [wCurrentMenuItem]
  2769. swap a
  2770. add b
  2771. ld [hl], a
  2772. .swapMovesInPartyMon
  2773. ld hl, wPartyMon1Moves
  2774. ld a, [wPlayerMonNumber]
  2775. ld bc, wPartyMon2 - wPartyMon1
  2776. call AddNTimes
  2777. push hl
  2778. call .swapBytes ; swap moves
  2779. pop hl
  2780. ld bc, wPartyMon1PP - wPartyMon1Moves
  2781. add hl, bc
  2782. call .swapBytes ; swap move PP
  2783. xor a
  2784. ld [wMenuItemToSwap], a ; deselect the item
  2785. jp MoveSelectionMenu
  2786. .swapBytes
  2787. push hl
  2788. ld a, [wMenuItemToSwap]
  2789. dec a
  2790. ld c, a
  2791. ld b, 0
  2792. add hl, bc
  2793. ld d, h
  2794. ld e, l
  2795. pop hl
  2796. ld a, [wCurrentMenuItem]
  2797. dec a
  2798. ld c, a
  2799. ld b, 0
  2800. add hl, bc
  2801. ld a, [de]
  2802. ld b, [hl]
  2803. ld [hl], a
  2804. ld a, b
  2805. ld [de], a
  2806. ret
  2807. .noMenuItemSelected
  2808. ld a, [wCurrentMenuItem]
  2809. ld [wMenuItemToSwap], a ; select the current menu item for swapping
  2810. jp MoveSelectionMenu
  2811. PrintMenuItem:
  2812. xor a
  2813. ld [H_AUTOBGTRANSFERENABLED], a
  2814. coord hl, 0, 8
  2815. ld b, 3
  2816. ld c, 9
  2817. call TextBoxBorder
  2818. ld a, [wPlayerDisabledMove]
  2819. and a
  2820. jr z, .notDisabled
  2821. swap a
  2822. and $f
  2823. ld b, a
  2824. ld a, [wCurrentMenuItem]
  2825. cp b
  2826. jr nz, .notDisabled
  2827. coord hl, 1, 10
  2828. ld de, DisabledText
  2829. call PlaceString
  2830. jr .moveDisabled
  2831. .notDisabled
  2832. ld hl, wCurrentMenuItem
  2833. dec [hl]
  2834. xor a
  2835. ld [H_WHOSETURN], a
  2836. ld hl, wBattleMonMoves
  2837. ld a, [wCurrentMenuItem]
  2838. ld c, a
  2839. ld b, $0 ; which item in the menu is the cursor pointing to? (0-3)
  2840. add hl, bc ; point to the item (move) in memory
  2841. ld a, [hl]
  2842. ld [wPlayerSelectedMove], a ; update wPlayerSelectedMove even if the move
  2843. ; isn't actually selected (just pointed to by the cursor)
  2844. ld a, [wPlayerMonNumber]
  2845. ld [wWhichPokemon], a
  2846. ld a, BATTLE_MON_DATA
  2847. ld [wMonDataLocation], a
  2848. callab GetMaxPP
  2849. ld hl, wCurrentMenuItem
  2850. ld c, [hl]
  2851. inc [hl]
  2852. ld b, $0
  2853. ld hl, wBattleMonPP
  2854. add hl, bc
  2855. ld a, [hl]
  2856. and $3f
  2857. ld [wcd6d], a
  2858. ; print TYPE/<type> and <curPP>/<maxPP>
  2859. coord hl, 1, 9
  2860. ld de, TypeText
  2861. call PlaceString
  2862. coord hl, 7, 11
  2863. ld [hl], "/"
  2864. coord hl, 5, 9
  2865. ld [hl], "/"
  2866. coord hl, 5, 11
  2867. ld de, wcd6d
  2868. lb bc, 1, 2
  2869. call PrintNumber
  2870. coord hl, 8, 11
  2871. ld de, wMaxPP
  2872. lb bc, 1, 2
  2873. call PrintNumber
  2874. call GetCurrentMove
  2875. coord hl, 2, 10
  2876. predef PrintMoveType
  2877. .moveDisabled
  2878. ld a, $1
  2879. ld [H_AUTOBGTRANSFERENABLED], a
  2880. jp Delay3
  2881. DisabledText:
  2882. db "disabled!@"
  2883. TypeText:
  2884. db "TYPE@"
  2885. SelectEnemyMove:
  2886. ld a, [wLinkState]
  2887. sub LINK_STATE_BATTLING
  2888. jr nz, .noLinkBattle
  2889. ; link battle
  2890. call SaveScreenTilesToBuffer1
  2891. call LinkBattleExchangeData
  2892. call LoadScreenTilesFromBuffer1
  2893. ld a, [wSerialExchangeNybbleReceiveData]
  2894. cp LINKBATTLE_STRUGGLE
  2895. jp z, .linkedOpponentUsedStruggle
  2896. cp LINKBATTLE_NO_ACTION
  2897. jr z, .unableToSelectMove
  2898. cp 4
  2899. ret nc
  2900. ld [wEnemyMoveListIndex], a
  2901. ld c, a
  2902. ld hl, wEnemyMonMoves
  2903. ld b, 0
  2904. add hl, bc
  2905. ld a, [hl]
  2906. jr .done
  2907. .noLinkBattle
  2908. ld a, [wEnemyBattleStatus2]
  2909. and (1 << NEEDS_TO_RECHARGE) | (1 << USING_RAGE) ; need to recharge or using rage
  2910. ret nz
  2911. ld hl, wEnemyBattleStatus1
  2912. ld a, [hl]
  2913. and (1 << CHARGING_UP) | (1 << THRASHING_ABOUT) ; using a charging move or thrash/petal dance
  2914. ret nz
  2915. ld a, [wEnemyMonStatus]
  2916. and SLP | 1 << FRZ ; sleeping or frozen
  2917. ret nz
  2918. ld a, [wEnemyBattleStatus1]
  2919. and (1 << USING_TRAPPING_MOVE) | (1 << STORING_ENERGY) ; using a trapping move like wrap or bide
  2920. ret nz
  2921. ld a, [wPlayerBattleStatus1]
  2922. bit USING_TRAPPING_MOVE, a ; caught in player's trapping move (e.g. wrap)
  2923. jr z, .canSelectMove
  2924. .unableToSelectMove
  2925. ld a, $ff
  2926. jr .done
  2927. .canSelectMove
  2928. ld hl, wEnemyMonMoves+1 ; 2nd enemy move
  2929. ld a, [hld]
  2930. and a
  2931. jr nz, .atLeastTwoMovesAvailable
  2932. ld a, [wEnemyDisabledMove]
  2933. and a
  2934. ld a, STRUGGLE ; struggle if the only move is disabled
  2935. jr nz, .done
  2936. .atLeastTwoMovesAvailable
  2937. ld a, [wIsInBattle]
  2938. dec a
  2939. jr z, .chooseRandomMove ; wild encounter
  2940. callab AIEnemyTrainerChooseMoves
  2941. .chooseRandomMove
  2942. push hl
  2943. call BattleRandom
  2944. ld b, $1
  2945. cp $3f ; select move 1, [0,3e] (63/256 chance)
  2946. jr c, .moveChosen
  2947. inc hl
  2948. inc b
  2949. cp $7f ; select move 2, [3f,7e] (64/256 chance)
  2950. jr c, .moveChosen
  2951. inc hl
  2952. inc b
  2953. cp $be ; select move 3, [7f,bd] (63/256 chance)
  2954. jr c, .moveChosen
  2955. inc hl
  2956. inc b ; select move 4, [be,ff] (66/256 chance)
  2957. .moveChosen
  2958. ld a, b
  2959. dec a
  2960. ld [wEnemyMoveListIndex], a
  2961. ld a, [wEnemyDisabledMove]
  2962. swap a
  2963. and $f
  2964. cp b
  2965. ld a, [hl]
  2966. pop hl
  2967. jr z, .chooseRandomMove ; move disabled, try again
  2968. and a
  2969. jr z, .chooseRandomMove ; move non-existant, try again
  2970. .done
  2971. ld [wEnemySelectedMove], a
  2972. ret
  2973. .linkedOpponentUsedStruggle
  2974. ld a, STRUGGLE
  2975. jr .done
  2976. ; this appears to exchange data with the other gameboy during link battles
  2977. LinkBattleExchangeData:
  2978. ld a, $ff
  2979. ld [wSerialExchangeNybbleReceiveData], a
  2980. ld a, [wPlayerMoveListIndex]
  2981. cp LINKBATTLE_RUN ; is the player running from battle?
  2982. jr z, .doExchange
  2983. ld a, [wActionResultOrTookBattleTurn]
  2984. and a ; is the player switching in another mon?
  2985. jr nz, .switching
  2986. ; the player used a move
  2987. ld a, [wPlayerSelectedMove]
  2988. cp STRUGGLE
  2989. ld b, LINKBATTLE_STRUGGLE
  2990. jr z, .next
  2991. dec b ; LINKBATTLE_NO_ACTION
  2992. inc a ; does move equal -1 (i.e. no action)?
  2993. jr z, .next
  2994. ld a, [wPlayerMoveListIndex]
  2995. jr .doExchange
  2996. .switching
  2997. ld a, [wWhichPokemon]
  2998. add 4
  2999. ld b, a
  3000. .next
  3001. ld a, b
  3002. .doExchange
  3003. ld [wSerialExchangeNybbleSendData], a
  3004. callab PrintWaitingText
  3005. .syncLoop1
  3006. call Serial_ExchangeNybble
  3007. call DelayFrame
  3008. ld a, [wSerialExchangeNybbleReceiveData]
  3009. inc a
  3010. jr z, .syncLoop1
  3011. ld b, 10
  3012. .syncLoop2
  3013. call DelayFrame
  3014. call Serial_ExchangeNybble
  3015. dec b
  3016. jr nz, .syncLoop2
  3017. ld b, 10
  3018. .syncLoop3
  3019. call DelayFrame
  3020. call Serial_SendZeroByte
  3021. dec b
  3022. jr nz, .syncLoop3
  3023. ret
  3024. ExecutePlayerMove:
  3025. xor a
  3026. ld [H_WHOSETURN], a ; set player's turn
  3027. ld a, [wPlayerSelectedMove]
  3028. inc a
  3029. jp z, ExecutePlayerMoveDone ; for selected move = FF, skip most of player's turn
  3030. xor a
  3031. ld [wMoveMissed], a
  3032. ld [wMonIsDisobedient], a
  3033. ld [wMoveDidntMiss], a
  3034. ld a, $a
  3035. ld [wDamageMultipliers], a
  3036. ld a, [wActionResultOrTookBattleTurn]
  3037. and a ; has the player already used the turn (e.g. by using an item, trying to run or switching pokemon)
  3038. jp nz, ExecutePlayerMoveDone
  3039. call PrintGhostText
  3040. jp z, ExecutePlayerMoveDone
  3041. call CheckPlayerStatusConditions
  3042. jr nz, .playerHasNoSpecialCondition
  3043. jp hl
  3044. .playerHasNoSpecialCondition
  3045. call GetCurrentMove
  3046. ld hl, wPlayerBattleStatus1
  3047. bit CHARGING_UP, [hl] ; charging up for attack
  3048. jr nz, PlayerCanExecuteChargingMove
  3049. call CheckForDisobedience
  3050. jp z, ExecutePlayerMoveDone
  3051. CheckIfPlayerNeedsToChargeUp:
  3052. ld a, [wPlayerMoveEffect]
  3053. cp CHARGE_EFFECT
  3054. jp z, JumpMoveEffect
  3055. cp FLY_EFFECT
  3056. jp z, JumpMoveEffect
  3057. jr PlayerCanExecuteMove
  3058. ; in-battle stuff
  3059. PlayerCanExecuteChargingMove:
  3060. ld hl, wPlayerBattleStatus1
  3061. res CHARGING_UP, [hl] ; reset charging up and invulnerability statuses if mon was charging up for an attack
  3062. ; being fully paralyzed or hurting oneself in confusion removes charging up status
  3063. ; resulting in the Pokemon being invulnerable for the whole battle
  3064. res INVULNERABLE, [hl]
  3065. PlayerCanExecuteMove:
  3066. call PrintMonName1Text
  3067. ld hl, DecrementPP
  3068. ld de, wPlayerSelectedMove ; pointer to the move just used
  3069. ld b, BANK(DecrementPP)
  3070. call Bankswitch
  3071. ld a, [wPlayerMoveEffect] ; effect of the move just used
  3072. ld hl, ResidualEffects1
  3073. ld de, 1
  3074. call IsInArray
  3075. jp c, JumpMoveEffect ; ResidualEffects1 moves skip damage calculation and accuracy tests
  3076. ; unless executed as part of their exclusive effect functions
  3077. ld a, [wPlayerMoveEffect]
  3078. ld hl, SpecialEffectsCont
  3079. ld de, 1
  3080. call IsInArray
  3081. call c, JumpMoveEffect ; execute the effects of SpecialEffectsCont moves (e.g. Wrap, Thrash) but don't skip anything
  3082. PlayerCalcMoveDamage:
  3083. ld a, [wPlayerMoveEffect]
  3084. ld hl, SetDamageEffects
  3085. ld de, 1
  3086. call IsInArray
  3087. jp c, .moveHitTest ; SetDamageEffects moves (e.g. Seismic Toss and Super Fang) skip damage calculation
  3088. call CriticalHitTest
  3089. call HandleCounterMove
  3090. jr z, handleIfPlayerMoveMissed
  3091. call GetDamageVarsForPlayerAttack
  3092. call CalculateDamage
  3093. jp z, playerCheckIfFlyOrChargeEffect ; for moves with 0 BP, skip any further damage calculation and, for now, skip MoveHitTest
  3094. ; for these moves, accuracy tests will only occur if they are called as part of the effect itself
  3095. call AdjustDamageForMoveType
  3096. call RandomizeDamage
  3097. .moveHitTest
  3098. call MoveHitTest
  3099. handleIfPlayerMoveMissed:
  3100. ld a, [wMoveMissed]
  3101. and a
  3102. jr z, getPlayerAnimationType
  3103. ld a, [wPlayerMoveEffect]
  3104. sub EXPLODE_EFFECT
  3105. jr z, playPlayerMoveAnimation ; don't play any animation if the move missed, unless it was EXPLODE_EFFECT
  3106. jr playerCheckIfFlyOrChargeEffect
  3107. getPlayerAnimationType:
  3108. ld a, [wPlayerMoveEffect]
  3109. and a
  3110. ld a, 4 ; move has no effect other than dealing damage
  3111. jr z, playPlayerMoveAnimation
  3112. ld a, 5 ; move has effect
  3113. playPlayerMoveAnimation:
  3114. push af
  3115. ld a, [wPlayerBattleStatus2]
  3116. bit HAS_SUBSTITUTE_UP, a
  3117. ld hl, HideSubstituteShowMonAnim
  3118. ld b, BANK(HideSubstituteShowMonAnim)
  3119. call nz, Bankswitch
  3120. pop af
  3121. ld [wAnimationType], a
  3122. ld a, [wPlayerMoveNum]
  3123. call PlayMoveAnimation
  3124. call HandleExplodingAnimation
  3125. call DrawPlayerHUDAndHPBar
  3126. ld a, [wPlayerBattleStatus2]
  3127. bit HAS_SUBSTITUTE_UP, a
  3128. ld hl, ReshowSubstituteAnim
  3129. ld b, BANK(ReshowSubstituteAnim)
  3130. call nz, Bankswitch
  3131. jr MirrorMoveCheck
  3132. playerCheckIfFlyOrChargeEffect:
  3133. ld c, 30
  3134. call DelayFrames
  3135. ld a, [wPlayerMoveEffect]
  3136. cp FLY_EFFECT
  3137. jr z, .playAnim
  3138. cp CHARGE_EFFECT
  3139. jr z, .playAnim
  3140. jr MirrorMoveCheck
  3141. .playAnim
  3142. xor a
  3143. ld [wAnimationType], a
  3144. ld a, STATUS_AFFECTED_ANIM
  3145. call PlayMoveAnimation
  3146. MirrorMoveCheck:
  3147. ld a, [wPlayerMoveEffect]
  3148. cp MIRROR_MOVE_EFFECT
  3149. jr nz, .metronomeCheck
  3150. call MirrorMoveCopyMove
  3151. jp z, ExecutePlayerMoveDone
  3152. xor a
  3153. ld [wMonIsDisobedient], a
  3154. jp CheckIfPlayerNeedsToChargeUp ; if Mirror Move was successful go back to damage calculation for copied move
  3155. .metronomeCheck
  3156. cp METRONOME_EFFECT
  3157. jr nz, .next
  3158. call MetronomePickMove
  3159. jp CheckIfPlayerNeedsToChargeUp ; Go back to damage calculation for the move picked by Metronome
  3160. .next
  3161. ld a, [wPlayerMoveEffect]
  3162. ld hl, ResidualEffects2
  3163. ld de, 1
  3164. call IsInArray
  3165. jp c, JumpMoveEffect ; done here after executing effects of ResidualEffects2
  3166. ld a, [wMoveMissed]
  3167. and a
  3168. jr z, .moveDidNotMiss
  3169. call PrintMoveFailureText
  3170. ld a, [wPlayerMoveEffect]
  3171. cp EXPLODE_EFFECT ; even if Explosion or Selfdestruct missed, its effect still needs to be activated
  3172. jr z, .notDone
  3173. jp ExecutePlayerMoveDone ; otherwise, we're done if the move missed
  3174. .moveDidNotMiss
  3175. call ApplyAttackToEnemyPokemon
  3176. call PrintCriticalOHKOText
  3177. callab DisplayEffectiveness
  3178. ld a, 1
  3179. ld [wMoveDidntMiss], a
  3180. .notDone
  3181. ld a, [wPlayerMoveEffect]
  3182. ld hl, AlwaysHappenSideEffects
  3183. ld de, 1
  3184. call IsInArray
  3185. call c, JumpMoveEffect ; not done after executing effects of AlwaysHappenSideEffects
  3186. ld hl, wEnemyMonHP
  3187. ld a, [hli]
  3188. ld b, [hl]
  3189. or b
  3190. ret z ; don't do anything else if the enemy fainted
  3191. call HandleBuildingRage
  3192. ld hl, wPlayerBattleStatus1
  3193. bit ATTACKING_MULTIPLE_TIMES, [hl]
  3194. jr z, .executeOtherEffects
  3195. ld a, [wPlayerNumAttacksLeft]
  3196. dec a
  3197. ld [wPlayerNumAttacksLeft], a
  3198. jp nz, getPlayerAnimationType ; for multi-hit moves, apply attack until PlayerNumAttacksLeft hits 0 or the enemy faints.
  3199. ; damage calculation and accuracy tests only happen for the first hit
  3200. res ATTACKING_MULTIPLE_TIMES, [hl] ; clear attacking multiple times status when all attacks are over
  3201. ld hl, MultiHitText
  3202. call PrintText
  3203. xor a
  3204. ld [wPlayerNumHits], a
  3205. .executeOtherEffects
  3206. ld a, [wPlayerMoveEffect]
  3207. and a
  3208. jp z, ExecutePlayerMoveDone
  3209. ld hl, SpecialEffects
  3210. ld de, 1
  3211. call IsInArray
  3212. call nc, JumpMoveEffect ; move effects not included in SpecialEffects or in either of the ResidualEffect arrays,
  3213. ; which are the effects not covered yet. Rage effect will be executed for a second time (though it's irrelevant).
  3214. ; Includes side effects that only need to be called if the target didn't faint.
  3215. ; Responsible for executing Twineedle's second side effect (poison).
  3216. jp ExecutePlayerMoveDone
  3217. MultiHitText:
  3218. TX_FAR _MultiHitText
  3219. db "@"
  3220. ExecutePlayerMoveDone:
  3221. xor a
  3222. ld [wActionResultOrTookBattleTurn], a
  3223. ld b, 1
  3224. ret
  3225. PrintGhostText:
  3226. ; print the ghost battle messages
  3227. call IsGhostBattle
  3228. ret nz
  3229. ld a, [H_WHOSETURN]
  3230. and a
  3231. jr nz, .Ghost
  3232. ld a, [wBattleMonStatus] ; player’s turn
  3233. and SLP | (1 << FRZ)
  3234. ret nz
  3235. ld hl, ScaredText
  3236. call PrintText
  3237. xor a
  3238. ret
  3239. .Ghost ; ghost’s turn
  3240. ld hl, GetOutText
  3241. call PrintText
  3242. xor a
  3243. ret
  3244. ScaredText:
  3245. TX_FAR _ScaredText
  3246. db "@"
  3247. GetOutText:
  3248. TX_FAR _GetOutText
  3249. db "@"
  3250. IsGhostBattle:
  3251. ld a, [wIsInBattle]
  3252. dec a
  3253. ret nz
  3254. ld a, [wCurMap]
  3255. cp POKEMONTOWER_1
  3256. jr c, .next
  3257. cp LAVENDER_HOUSE_1
  3258. jr nc, .next
  3259. ld b, SILPH_SCOPE
  3260. call IsItemInBag
  3261. ret z
  3262. .next
  3263. ld a, 1
  3264. and a
  3265. ret
  3266. ; checks for various status conditions affecting the player mon
  3267. ; stores whether the mon cannot use a move this turn in Z flag
  3268. CheckPlayerStatusConditions:
  3269. ld hl, wBattleMonStatus
  3270. ld a, [hl]
  3271. and SLP ; sleep mask
  3272. jr z, .FrozenCheck
  3273. ; sleeping
  3274. dec a
  3275. ld [wBattleMonStatus], a ; decrement number of turns left
  3276. and a
  3277. jr z, .WakeUp ; if the number of turns hit 0, wake up
  3278. ; fast asleep
  3279. xor a
  3280. ld [wAnimationType], a
  3281. ld a, SLP_ANIM - 1
  3282. call PlayMoveAnimation
  3283. ld hl, FastAsleepText
  3284. call PrintText
  3285. jr .sleepDone
  3286. .WakeUp
  3287. ld hl, WokeUpText
  3288. call PrintText
  3289. .sleepDone
  3290. xor a
  3291. ld [wPlayerUsedMove], a
  3292. ld hl, ExecutePlayerMoveDone ; player can't move this turn
  3293. jp .returnToHL
  3294. .FrozenCheck
  3295. bit FRZ, [hl] ; frozen?
  3296. jr z, .HeldInPlaceCheck
  3297. ld hl, IsFrozenText
  3298. call PrintText
  3299. xor a
  3300. ld [wPlayerUsedMove], a
  3301. ld hl, ExecutePlayerMoveDone ; player can't move this turn
  3302. jp .returnToHL
  3303. .HeldInPlaceCheck
  3304. ld a, [wEnemyBattleStatus1]
  3305. bit USING_TRAPPING_MOVE, a ; is enemy using a mult-turn move like wrap?
  3306. jp z, .FlinchedCheck
  3307. ld hl, CantMoveText
  3308. call PrintText
  3309. ld hl, ExecutePlayerMoveDone ; player can't move this turn
  3310. jp .returnToHL
  3311. .FlinchedCheck
  3312. ld hl, wPlayerBattleStatus1
  3313. bit FLINCHED, [hl]
  3314. jp z, .HyperBeamCheck
  3315. res FLINCHED, [hl] ; reset player's flinch status
  3316. ld hl, FlinchedText
  3317. call PrintText
  3318. ld hl, ExecutePlayerMoveDone ; player can't move this turn
  3319. jp .returnToHL
  3320. .HyperBeamCheck
  3321. ld hl, wPlayerBattleStatus2
  3322. bit NEEDS_TO_RECHARGE, [hl]
  3323. jr z, .AnyMoveDisabledCheck
  3324. res NEEDS_TO_RECHARGE, [hl] ; reset player's recharge status
  3325. ld hl, MustRechargeText
  3326. call PrintText
  3327. ld hl, ExecutePlayerMoveDone ; player can't move this turn
  3328. jp .returnToHL
  3329. .AnyMoveDisabledCheck
  3330. ld hl, wPlayerDisabledMove
  3331. ld a, [hl]
  3332. and a
  3333. jr z, .ConfusedCheck
  3334. dec a
  3335. ld [hl], a
  3336. and $f ; did Disable counter hit 0?
  3337. jr nz, .ConfusedCheck
  3338. ld [hl], a
  3339. ld [wPlayerDisabledMoveNumber], a
  3340. ld hl, DisabledNoMoreText
  3341. call PrintText
  3342. .ConfusedCheck
  3343. ld a, [wPlayerBattleStatus1]
  3344. add a ; is player confused?
  3345. jr nc, .TriedToUseDisabledMoveCheck
  3346. ld hl, wPlayerConfusedCounter
  3347. dec [hl]
  3348. jr nz, .IsConfused
  3349. ld hl, wPlayerBattleStatus1
  3350. res CONFUSED, [hl] ; if confused counter hit 0, reset confusion status
  3351. ld hl, ConfusedNoMoreText
  3352. call PrintText
  3353. jr .TriedToUseDisabledMoveCheck
  3354. .IsConfused
  3355. ld hl, IsConfusedText
  3356. call PrintText
  3357. xor a
  3358. ld [wAnimationType], a
  3359. ld a, CONF_ANIM - 1
  3360. call PlayMoveAnimation
  3361. call BattleRandom
  3362. cp $80 ; 50% chance to hurt itself
  3363. jr c, .TriedToUseDisabledMoveCheck
  3364. ld hl, wPlayerBattleStatus1
  3365. ld a, [hl]
  3366. and 1 << CONFUSED ; if mon hurts itself, clear every other status from wPlayerBattleStatus1
  3367. ld [hl], a
  3368. call HandleSelfConfusionDamage
  3369. jr .MonHurtItselfOrFullyParalysed
  3370. .TriedToUseDisabledMoveCheck
  3371. ; prevents a disabled move that was selected before being disabled from being used
  3372. ld a, [wPlayerDisabledMoveNumber]
  3373. and a
  3374. jr z, .ParalysisCheck
  3375. ld hl, wPlayerSelectedMove
  3376. cp [hl]
  3377. jr nz, .ParalysisCheck
  3378. call PrintMoveIsDisabledText
  3379. ld hl, ExecutePlayerMoveDone ; if a disabled move was somehow selected, player can't move this turn
  3380. jp .returnToHL
  3381. .ParalysisCheck
  3382. ld hl, wBattleMonStatus
  3383. bit PAR, [hl]
  3384. jr z, .BideCheck
  3385. call BattleRandom
  3386. cp $3F ; 25% to be fully paralyzed
  3387. jr nc, .BideCheck
  3388. ld hl, FullyParalyzedText
  3389. call PrintText
  3390. .MonHurtItselfOrFullyParalysed
  3391. ld hl, wPlayerBattleStatus1
  3392. ld a, [hl]
  3393. ; clear bide, thrashing, charging up, and trapping moves such as warp (already cleared for confusion damage)
  3394. and $ff ^ ((1 << STORING_ENERGY) | (1 << THRASHING_ABOUT) | (1 << CHARGING_UP) | (1 << USING_TRAPPING_MOVE))
  3395. ld [hl], a
  3396. ld a, [wPlayerMoveEffect]
  3397. cp FLY_EFFECT
  3398. jr z, .FlyOrChargeEffect
  3399. cp CHARGE_EFFECT
  3400. jr z, .FlyOrChargeEffect
  3401. jr .NotFlyOrChargeEffect
  3402. .FlyOrChargeEffect
  3403. xor a
  3404. ld [wAnimationType], a
  3405. ld a, STATUS_AFFECTED_ANIM
  3406. call PlayMoveAnimation
  3407. .NotFlyOrChargeEffect
  3408. ld hl, ExecutePlayerMoveDone
  3409. jp .returnToHL ; if using a two-turn move, we need to recharge the first turn
  3410. .BideCheck
  3411. ld hl, wPlayerBattleStatus1
  3412. bit STORING_ENERGY, [hl] ; is mon using bide?
  3413. jr z, .ThrashingAboutCheck
  3414. xor a
  3415. ld [wPlayerMoveNum], a
  3416. ld hl, wDamage
  3417. ld a, [hli]
  3418. ld b, a
  3419. ld c, [hl]
  3420. ld hl, wPlayerBideAccumulatedDamage + 1
  3421. ld a, [hl]
  3422. add c ; accumulate damage taken
  3423. ld [hld], a
  3424. ld a, [hl]
  3425. adc b
  3426. ld [hl], a
  3427. ld hl, wPlayerNumAttacksLeft
  3428. dec [hl] ; did Bide counter hit 0?
  3429. jr z, .UnleashEnergy
  3430. ld hl, ExecutePlayerMoveDone
  3431. jp .returnToHL ; unless mon unleashes energy, can't move this turn
  3432. .UnleashEnergy
  3433. ld hl, wPlayerBattleStatus1
  3434. res STORING_ENERGY, [hl] ; not using bide any more
  3435. ld hl, UnleashedEnergyText
  3436. call PrintText
  3437. ld a, 1
  3438. ld [wPlayerMovePower], a
  3439. ld hl, wPlayerBideAccumulatedDamage + 1
  3440. ld a, [hld]
  3441. add a
  3442. ld b, a
  3443. ld [wDamage + 1], a
  3444. ld a, [hl]
  3445. rl a ; double the damage
  3446. ld [wDamage], a
  3447. or b
  3448. jr nz, .next
  3449. ld a, 1
  3450. ld [wMoveMissed], a
  3451. .next
  3452. xor a
  3453. ld [hli], a
  3454. ld [hl], a
  3455. ld a, BIDE
  3456. ld [wPlayerMoveNum], a
  3457. ld hl, handleIfPlayerMoveMissed ; skip damage calculation, DecrementPP and MoveHitTest
  3458. jp .returnToHL
  3459. .ThrashingAboutCheck
  3460. bit THRASHING_ABOUT, [hl] ; is mon using thrash or petal dance?
  3461. jr z, .MultiturnMoveCheck
  3462. ld a, THRASH
  3463. ld [wPlayerMoveNum], a
  3464. ld hl, ThrashingAboutText
  3465. call PrintText
  3466. ld hl, wPlayerNumAttacksLeft
  3467. dec [hl] ; did Thrashing About counter hit 0?
  3468. ld hl, PlayerCalcMoveDamage ; skip DecrementPP
  3469. jp nz, .returnToHL
  3470. push hl
  3471. ld hl, wPlayerBattleStatus1
  3472. res THRASHING_ABOUT, [hl] ; no longer thrashing about
  3473. set CONFUSED, [hl] ; confused
  3474. call BattleRandom
  3475. and 3
  3476. inc a
  3477. inc a ; confused for 2-5 turns
  3478. ld [wPlayerConfusedCounter], a
  3479. pop hl ; skip DecrementPP
  3480. jp .returnToHL
  3481. .MultiturnMoveCheck
  3482. bit USING_TRAPPING_MOVE, [hl] ; is mon using multi-turn move?
  3483. jp z, .RageCheck
  3484. ld hl, AttackContinuesText
  3485. call PrintText
  3486. ld a, [wPlayerNumAttacksLeft]
  3487. dec a ; did multi-turn move end?
  3488. ld [wPlayerNumAttacksLeft], a
  3489. ld hl, getPlayerAnimationType ; if it didn't, skip damage calculation (deal damage equal to last hit),
  3490. ; DecrementPP and MoveHitTest
  3491. jp nz, .returnToHL
  3492. jp .returnToHL
  3493. .RageCheck
  3494. ld a, [wPlayerBattleStatus2]
  3495. bit USING_RAGE, a ; is mon using rage?
  3496. jp z, .checkPlayerStatusConditionsDone ; if we made it this far, mon can move normally this turn
  3497. ld a, RAGE
  3498. ld [wd11e], a
  3499. call GetMoveName
  3500. call CopyStringToCF4B
  3501. xor a
  3502. ld [wPlayerMoveEffect], a
  3503. ld hl, PlayerCanExecuteMove
  3504. jp .returnToHL
  3505. .returnToHL
  3506. xor a
  3507. ret
  3508. .checkPlayerStatusConditionsDone
  3509. ld a, $1
  3510. and a
  3511. ret
  3512. FastAsleepText:
  3513. TX_FAR _FastAsleepText
  3514. db "@"
  3515. WokeUpText:
  3516. TX_FAR _WokeUpText
  3517. db "@"
  3518. IsFrozenText:
  3519. TX_FAR _IsFrozenText
  3520. db "@"
  3521. FullyParalyzedText:
  3522. TX_FAR _FullyParalyzedText
  3523. db "@"
  3524. FlinchedText:
  3525. TX_FAR _FlinchedText
  3526. db "@"
  3527. MustRechargeText:
  3528. TX_FAR _MustRechargeText
  3529. db "@"
  3530. DisabledNoMoreText:
  3531. TX_FAR _DisabledNoMoreText
  3532. db "@"
  3533. IsConfusedText:
  3534. TX_FAR _IsConfusedText
  3535. db "@"
  3536. HurtItselfText:
  3537. TX_FAR _HurtItselfText
  3538. db "@"
  3539. ConfusedNoMoreText:
  3540. TX_FAR _ConfusedNoMoreText
  3541. db "@"
  3542. SavingEnergyText:
  3543. TX_FAR _SavingEnergyText
  3544. db "@"
  3545. UnleashedEnergyText:
  3546. TX_FAR _UnleashedEnergyText
  3547. db "@"
  3548. ThrashingAboutText:
  3549. TX_FAR _ThrashingAboutText
  3550. db "@"
  3551. AttackContinuesText:
  3552. TX_FAR _AttackContinuesText
  3553. db "@"
  3554. CantMoveText:
  3555. TX_FAR _CantMoveText
  3556. db "@"
  3557. PrintMoveIsDisabledText:
  3558. ld hl, wPlayerSelectedMove
  3559. ld de, wPlayerBattleStatus1
  3560. ld a, [H_WHOSETURN]
  3561. and a
  3562. jr z, .removeChargingUp
  3563. inc hl
  3564. ld de, wEnemyBattleStatus1
  3565. .removeChargingUp
  3566. ld a, [de]
  3567. res CHARGING_UP, a ; end the pokemon's
  3568. ld [de], a
  3569. ld a, [hl]
  3570. ld [wd11e], a
  3571. call GetMoveName
  3572. ld hl, MoveIsDisabledText
  3573. jp PrintText
  3574. MoveIsDisabledText:
  3575. TX_FAR _MoveIsDisabledText
  3576. db "@"
  3577. HandleSelfConfusionDamage:
  3578. ld hl, HurtItselfText
  3579. call PrintText
  3580. ld hl, wEnemyMonDefense
  3581. ld a, [hli]
  3582. push af
  3583. ld a, [hld]
  3584. push af
  3585. ld a, [wBattleMonDefense]
  3586. ld [hli], a
  3587. ld a, [wBattleMonDefense + 1]
  3588. ld [hl], a
  3589. ld hl, wPlayerMoveEffect
  3590. push hl
  3591. ld a, [hl]
  3592. push af
  3593. xor a
  3594. ld [hli], a
  3595. ld [wCriticalHitOrOHKO], a ; self-inflicted confusion damage can't be a Critical Hit
  3596. ld a, 40 ; 40 base power
  3597. ld [hli], a
  3598. xor a
  3599. ld [hl], a
  3600. call GetDamageVarsForPlayerAttack
  3601. call CalculateDamage ; ignores AdjustDamageForMoveType (type-less damage), RandomizeDamage,
  3602. ; and MoveHitTest (always hits)
  3603. pop af
  3604. pop hl
  3605. ld [hl], a
  3606. ld hl, wEnemyMonDefense + 1
  3607. pop af
  3608. ld [hld], a
  3609. pop af
  3610. ld [hl], a
  3611. xor a
  3612. ld [wAnimationType], a
  3613. inc a
  3614. ld [H_WHOSETURN], a
  3615. call PlayMoveAnimation
  3616. call DrawPlayerHUDAndHPBar
  3617. xor a
  3618. ld [H_WHOSETURN], a
  3619. jp ApplyDamageToPlayerPokemon
  3620. PrintMonName1Text:
  3621. ld hl, MonName1Text
  3622. jp PrintText
  3623. ; this function wastes time calling DetermineExclamationPointTextNum
  3624. ; and choosing between Used1Text and Used2Text, even though
  3625. ; those text strings are identical and both continue at PrintInsteadText
  3626. ; this likely had to do with Japanese grammar that got translated,
  3627. ; but the functionality didn't get removed
  3628. MonName1Text:
  3629. TX_FAR _MonName1Text
  3630. TX_ASM
  3631. ld a, [H_WHOSETURN]
  3632. and a
  3633. ld a, [wPlayerMoveNum]
  3634. ld hl, wPlayerUsedMove
  3635. jr z, .playerTurn
  3636. ld a, [wEnemyMoveNum]
  3637. ld hl, wEnemyUsedMove
  3638. .playerTurn
  3639. ld [hl], a
  3640. ld [wd11e], a
  3641. call DetermineExclamationPointTextNum
  3642. ld a, [wMonIsDisobedient]
  3643. and a
  3644. ld hl, Used2Text
  3645. ret nz
  3646. ld a, [wd11e]
  3647. cp 3
  3648. ld hl, Used2Text
  3649. ret c
  3650. ld hl, Used1Text
  3651. ret
  3652. Used1Text:
  3653. TX_FAR _Used1Text
  3654. TX_ASM
  3655. jr PrintInsteadText
  3656. Used2Text:
  3657. TX_FAR _Used2Text
  3658. TX_ASM
  3659. ; fall through
  3660. PrintInsteadText:
  3661. ld a, [wMonIsDisobedient]
  3662. and a
  3663. jr z, PrintMoveName
  3664. ld hl, InsteadText
  3665. ret
  3666. InsteadText:
  3667. TX_FAR _InsteadText
  3668. TX_ASM
  3669. ; fall through
  3670. PrintMoveName:
  3671. ld hl, _PrintMoveName
  3672. ret
  3673. _PrintMoveName:
  3674. TX_FAR _CF4BText
  3675. TX_ASM
  3676. ld hl, ExclamationPointPointerTable
  3677. ld a, [wd11e] ; exclamation point num
  3678. add a
  3679. push bc
  3680. ld b, $0
  3681. ld c, a
  3682. add hl, bc
  3683. pop bc
  3684. ld a, [hli]
  3685. ld h, [hl]
  3686. ld l, a
  3687. ret
  3688. ExclamationPointPointerTable:
  3689. dw ExclamationPoint1Text
  3690. dw ExclamationPoint2Text
  3691. dw ExclamationPoint3Text
  3692. dw ExclamationPoint4Text
  3693. dw ExclamationPoint5Text
  3694. ExclamationPoint1Text:
  3695. TX_FAR _ExclamationPoint1Text
  3696. db "@"
  3697. ExclamationPoint2Text:
  3698. TX_FAR _ExclamationPoint2Text
  3699. db "@"
  3700. ExclamationPoint3Text:
  3701. TX_FAR _ExclamationPoint3Text
  3702. db "@"
  3703. ExclamationPoint4Text:
  3704. TX_FAR _ExclamationPoint4Text
  3705. db "@"
  3706. ExclamationPoint5Text:
  3707. TX_FAR _ExclamationPoint5Text
  3708. db "@"
  3709. ; this function does nothing useful
  3710. ; if the move being used is in set [1-4] from ExclamationPointMoveSets,
  3711. ; use ExclamationPoint[1-4]Text
  3712. ; otherwise, use ExclamationPoint5Text
  3713. ; but all five text strings are identical
  3714. ; this likely had to do with Japanese grammar that got translated,
  3715. ; but the functionality didn't get removed
  3716. DetermineExclamationPointTextNum:
  3717. push bc
  3718. ld a, [wd11e] ; move ID
  3719. ld c, a
  3720. ld b, $0
  3721. ld hl, ExclamationPointMoveSets
  3722. .loop
  3723. ld a, [hli]
  3724. cp $ff
  3725. jr z, .done
  3726. cp c
  3727. jr z, .done
  3728. and a
  3729. jr nz, .loop
  3730. inc b
  3731. jr .loop
  3732. .done
  3733. ld a, b
  3734. ld [wd11e], a ; exclamation point num
  3735. pop bc
  3736. ret
  3737. ExclamationPointMoveSets:
  3738. db SWORDS_DANCE, GROWTH
  3739. db $00
  3740. db RECOVER, BIDE, SELFDESTRUCT, AMNESIA
  3741. db $00
  3742. db MEDITATE, AGILITY, TELEPORT, MIMIC, DOUBLE_TEAM, BARRAGE
  3743. db $00
  3744. db POUND, SCRATCH, VICEGRIP, WING_ATTACK, FLY, BIND, SLAM, HORN_ATTACK, BODY_SLAM
  3745. db WRAP, THRASH, TAIL_WHIP, LEER, BITE, GROWL, ROAR, SING, PECK, COUNTER
  3746. db STRENGTH, ABSORB, STRING_SHOT, EARTHQUAKE, FISSURE, DIG, TOXIC, SCREECH, HARDEN
  3747. db MINIMIZE, WITHDRAW, DEFENSE_CURL, METRONOME, LICK, CLAMP, CONSTRICT, POISON_GAS
  3748. db LEECH_LIFE, BUBBLE, FLASH, SPLASH, ACID_ARMOR, FURY_SWIPES, REST, SHARPEN, SLASH, SUBSTITUTE
  3749. db $00
  3750. db $FF ; terminator
  3751. PrintMoveFailureText:
  3752. ld de, wPlayerMoveEffect
  3753. ld a, [H_WHOSETURN]
  3754. and a
  3755. jr z, .playersTurn
  3756. ld de, wEnemyMoveEffect
  3757. .playersTurn
  3758. ld hl, DoesntAffectMonText
  3759. ld a, [wDamageMultipliers]
  3760. and $7f
  3761. jr z, .gotTextToPrint
  3762. ld hl, AttackMissedText
  3763. ld a, [wCriticalHitOrOHKO]
  3764. cp $ff
  3765. jr nz, .gotTextToPrint
  3766. ld hl, UnaffectedText
  3767. .gotTextToPrint
  3768. push de
  3769. call PrintText
  3770. xor a
  3771. ld [wCriticalHitOrOHKO], a
  3772. pop de
  3773. ld a, [de]
  3774. cp JUMP_KICK_EFFECT
  3775. ret nz
  3776. ; if you get here, the mon used jump kick or hi jump kick and missed
  3777. ld hl, wDamage ; since the move missed, wDamage will always contain 0 at this point.
  3778. ; Thus, recoil damage will always be equal to 1
  3779. ; even if it was intended to be potential damage/8.
  3780. ld a, [hli]
  3781. ld b, [hl]
  3782. srl a
  3783. rr b
  3784. srl a
  3785. rr b
  3786. srl a
  3787. rr b
  3788. ld [hl], b
  3789. dec hl
  3790. ld [hli], a
  3791. or b
  3792. jr nz, .applyRecoil
  3793. inc a
  3794. ld [hl], a
  3795. .applyRecoil
  3796. ld hl, KeptGoingAndCrashedText
  3797. call PrintText
  3798. ld b, $4
  3799. predef PredefShakeScreenHorizontally
  3800. ld a, [H_WHOSETURN]
  3801. and a
  3802. jr nz, .enemyTurn
  3803. jp ApplyDamageToPlayerPokemon
  3804. .enemyTurn
  3805. jp ApplyDamageToEnemyPokemon
  3806. AttackMissedText:
  3807. TX_FAR _AttackMissedText
  3808. db "@"
  3809. KeptGoingAndCrashedText:
  3810. TX_FAR _KeptGoingAndCrashedText
  3811. db "@"
  3812. UnaffectedText:
  3813. TX_FAR _UnaffectedText
  3814. db "@"
  3815. PrintDoesntAffectText:
  3816. ld hl, DoesntAffectMonText
  3817. jp PrintText
  3818. DoesntAffectMonText:
  3819. TX_FAR _DoesntAffectMonText
  3820. db "@"
  3821. ; if there was a critical hit or an OHKO was successful, print the corresponding text
  3822. PrintCriticalOHKOText:
  3823. ld a, [wCriticalHitOrOHKO]
  3824. and a
  3825. jr z, .done ; do nothing if there was no critical hit or successful OHKO
  3826. dec a
  3827. add a
  3828. ld hl, CriticalOHKOTextPointers
  3829. ld b, $0
  3830. ld c, a
  3831. add hl, bc
  3832. ld a, [hli]
  3833. ld h, [hl]
  3834. ld l, a
  3835. call PrintText
  3836. xor a
  3837. ld [wCriticalHitOrOHKO], a
  3838. .done
  3839. ld c, 20
  3840. jp DelayFrames
  3841. CriticalOHKOTextPointers:
  3842. dw CriticalHitText
  3843. dw OHKOText
  3844. CriticalHitText:
  3845. TX_FAR _CriticalHitText
  3846. db "@"
  3847. OHKOText:
  3848. TX_FAR _OHKOText
  3849. db "@"
  3850. ; checks if a traded mon will disobey due to lack of badges
  3851. ; stores whether the mon will use a move in Z flag
  3852. CheckForDisobedience:
  3853. xor a
  3854. ld [wMonIsDisobedient], a
  3855. ld a, [wLinkState]
  3856. cp LINK_STATE_BATTLING
  3857. jr nz, .checkIfMonIsTraded
  3858. ld a, $1
  3859. and a
  3860. ret
  3861. ; compare the mon's original trainer ID with the player's ID to see if it was traded
  3862. .checkIfMonIsTraded
  3863. ld hl, wPartyMon1OTID
  3864. ld bc, wPartyMon2 - wPartyMon1
  3865. ld a, [wPlayerMonNumber]
  3866. call AddNTimes
  3867. ld a, [wPlayerID]
  3868. cp [hl]
  3869. jr nz, .monIsTraded
  3870. inc hl
  3871. ld a, [wPlayerID + 1]
  3872. cp [hl]
  3873. jp z, .canUseMove
  3874. ; it was traded
  3875. .monIsTraded
  3876. ; what level might disobey?
  3877. ld hl, wObtainedBadges
  3878. bit 7, [hl]
  3879. ld a, 101
  3880. jr nz, .next
  3881. bit 5, [hl]
  3882. ld a, 70
  3883. jr nz, .next
  3884. bit 3, [hl]
  3885. ld a, 50
  3886. jr nz, .next
  3887. bit 1, [hl]
  3888. ld a, 30
  3889. jr nz, .next
  3890. ld a, 10
  3891. .next
  3892. ld b, a
  3893. ld c, a
  3894. ld a, [wBattleMonLevel]
  3895. ld d, a
  3896. add b
  3897. ld b, a
  3898. jr nc, .noCarry
  3899. ld b, $ff ; cap b at $ff
  3900. .noCarry
  3901. ld a, c
  3902. cp d
  3903. jp nc, .canUseMove
  3904. .loop1
  3905. call BattleRandom
  3906. swap a
  3907. cp b
  3908. jr nc, .loop1
  3909. cp c
  3910. jp c, .canUseMove
  3911. .loop2
  3912. call BattleRandom
  3913. cp b
  3914. jr nc, .loop2
  3915. cp c
  3916. jr c, .useRandomMove
  3917. ld a, d
  3918. sub c
  3919. ld b, a
  3920. call BattleRandom
  3921. swap a
  3922. sub b
  3923. jr c, .monNaps
  3924. cp b
  3925. jr nc, .monDoesNothing
  3926. ld hl, WontObeyText
  3927. call PrintText
  3928. call HandleSelfConfusionDamage
  3929. jp .cannotUseMove
  3930. .monNaps
  3931. call BattleRandom
  3932. add a
  3933. swap a
  3934. and SLP ; sleep mask
  3935. jr z, .monNaps ; keep trying until we get at least 1 turn of sleep
  3936. ld [wBattleMonStatus], a
  3937. ld hl, BeganToNapText
  3938. jr .printText
  3939. .monDoesNothing
  3940. call BattleRandom
  3941. and $3
  3942. ld hl, LoafingAroundText
  3943. and a
  3944. jr z, .printText
  3945. ld hl, WontObeyText
  3946. dec a
  3947. jr z, .printText
  3948. ld hl, TurnedAwayText
  3949. dec a
  3950. jr z, .printText
  3951. ld hl, IgnoredOrdersText
  3952. .printText
  3953. call PrintText
  3954. jr .cannotUseMove
  3955. .useRandomMove
  3956. ld a, [wBattleMonMoves + 1]
  3957. and a ; is the second move slot empty?
  3958. jr z, .monDoesNothing ; mon will not use move if it only knows one move
  3959. ld a, [wPlayerDisabledMoveNumber]
  3960. and a
  3961. jr nz, .monDoesNothing
  3962. ld a, [wPlayerSelectedMove]
  3963. cp STRUGGLE
  3964. jr z, .monDoesNothing ; mon will not use move if struggling
  3965. ; check if only one move has remaining PP
  3966. ld hl, wBattleMonPP
  3967. push hl
  3968. ld a, [hli]
  3969. and $3f
  3970. ld b, a
  3971. ld a, [hli]
  3972. and $3f
  3973. add b
  3974. ld b, a
  3975. ld a, [hli]
  3976. and $3f
  3977. add b
  3978. ld b, a
  3979. ld a, [hl]
  3980. and $3f
  3981. add b
  3982. pop hl
  3983. push af
  3984. ld a, [wCurrentMenuItem]
  3985. ld c, a
  3986. ld b, $0
  3987. add hl, bc
  3988. ld a, [hl]
  3989. and $3f
  3990. ld b, a
  3991. pop af
  3992. cp b
  3993. jr z, .monDoesNothing ; mon will not use move if only one move has remaining PP
  3994. ld a, $1
  3995. ld [wMonIsDisobedient], a
  3996. ld a, [wMaxMenuItem]
  3997. ld b, a
  3998. ld a, [wCurrentMenuItem]
  3999. ld c, a
  4000. .chooseMove
  4001. call BattleRandom
  4002. and $3
  4003. cp b
  4004. jr nc, .chooseMove ; if the random number is greater than the move count, choose another
  4005. cp c
  4006. jr z, .chooseMove ; if the random number matches the move the player selected, choose another
  4007. ld [wCurrentMenuItem], a
  4008. ld hl, wBattleMonPP
  4009. ld e, a
  4010. ld d, $0
  4011. add hl, de
  4012. ld a, [hl]
  4013. and a ; does the move have any PP left?
  4014. jr z, .chooseMove ; if the move has no PP left, choose another
  4015. ld a, [wCurrentMenuItem]
  4016. ld c, a
  4017. ld b, $0
  4018. ld hl, wBattleMonMoves
  4019. add hl, bc
  4020. ld a, [hl]
  4021. ld [wPlayerSelectedMove], a
  4022. call GetCurrentMove
  4023. .canUseMove
  4024. ld a, $1
  4025. and a; clear Z flag
  4026. ret
  4027. .cannotUseMove
  4028. xor a ; set Z flag
  4029. ret
  4030. LoafingAroundText:
  4031. TX_FAR _LoafingAroundText
  4032. db "@"
  4033. BeganToNapText:
  4034. TX_FAR _BeganToNapText
  4035. db "@"
  4036. WontObeyText:
  4037. TX_FAR _WontObeyText
  4038. db "@"
  4039. TurnedAwayText:
  4040. TX_FAR _TurnedAwayText
  4041. db "@"
  4042. IgnoredOrdersText:
  4043. TX_FAR _IgnoredOrdersText
  4044. db "@"
  4045. ; sets b, c, d, and e for the CalculateDamage routine in the case of an attack by the player mon
  4046. GetDamageVarsForPlayerAttack:
  4047. xor a
  4048. ld hl, wDamage ; damage to eventually inflict, initialise to zero
  4049. ldi [hl], a
  4050. ld [hl], a
  4051. ld hl, wPlayerMovePower
  4052. ld a, [hli]
  4053. and a
  4054. ld d, a ; d = move power
  4055. ret z ; return if move power is zero
  4056. ld a, [hl] ; a = [wPlayerMoveType]
  4057. cp FIRE ; types >= FIRE are all special
  4058. jr nc, .specialAttack
  4059. .physicalAttack
  4060. ld hl, wEnemyMonDefense
  4061. ld a, [hli]
  4062. ld b, a
  4063. ld c, [hl] ; bc = enemy defense
  4064. ld a, [wEnemyBattleStatus3]
  4065. bit HAS_REFLECT_UP, a ; check for Reflect
  4066. jr z, .physicalAttackCritCheck
  4067. ; if the enemy has used Reflect, double the enemy's defense
  4068. sla c
  4069. rl b
  4070. .physicalAttackCritCheck
  4071. ld hl, wBattleMonAttack
  4072. ld a, [wCriticalHitOrOHKO]
  4073. and a ; check for critical hit
  4074. jr z, .scaleStats
  4075. ; in the case of a critical hit, reset the player's attack and the enemy's defense to their base values
  4076. ld c, 3 ; defense stat
  4077. call GetEnemyMonStat
  4078. ld a, [H_PRODUCT + 2]
  4079. ld b, a
  4080. ld a, [H_PRODUCT + 3]
  4081. ld c, a
  4082. push bc
  4083. ld hl, wPartyMon1Attack
  4084. ld a, [wPlayerMonNumber]
  4085. ld bc, wPartyMon2 - wPartyMon1
  4086. call AddNTimes
  4087. pop bc
  4088. jr .scaleStats
  4089. .specialAttack
  4090. ld hl, wEnemyMonSpecial
  4091. ld a, [hli]
  4092. ld b, a
  4093. ld c, [hl] ; bc = enemy special
  4094. ld a, [wEnemyBattleStatus3]
  4095. bit HAS_LIGHT_SCREEN_UP, a ; check for Light Screen
  4096. jr z, .specialAttackCritCheck
  4097. ; if the enemy has used Light Screen, double the enemy's special
  4098. sla c
  4099. rl b
  4100. ; reflect and light screen boosts do not cap the stat at 999, so weird things will happen during stats scaling if
  4101. ; a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
  4102. .specialAttackCritCheck
  4103. ld hl, wBattleMonSpecial
  4104. ld a, [wCriticalHitOrOHKO]
  4105. and a ; check for critical hit
  4106. jr z, .scaleStats
  4107. ; in the case of a critical hit, reset the player's and enemy's specials to their base values
  4108. ld c, 5 ; special stat
  4109. call GetEnemyMonStat
  4110. ld a, [H_PRODUCT + 2]
  4111. ld b, a
  4112. ld a, [H_PRODUCT + 3]
  4113. ld c, a
  4114. push bc
  4115. ld hl, wPartyMon1Special
  4116. ld a, [wPlayerMonNumber]
  4117. ld bc, wPartyMon2 - wPartyMon1
  4118. call AddNTimes
  4119. pop bc
  4120. ; if either the offensive or defensive stat is too large to store in a byte, scale both stats by dividing them by 4
  4121. ; this allows values with up to 10 bits (values up to 1023) to be handled
  4122. ; anything larger will wrap around
  4123. .scaleStats
  4124. ld a, [hli]
  4125. ld l, [hl]
  4126. ld h, a ; hl = player's offensive stat
  4127. or b ; is either high byte nonzero?
  4128. jr z, .next ; if not, we don't need to scale
  4129. ; bc /= 4 (scale enemy's defensive stat)
  4130. srl b
  4131. rr c
  4132. srl b
  4133. rr c
  4134. ; defensive stat can actually end up as 0, leading to a division by 0 freeze during damage calculation
  4135. ; hl /= 4 (scale player's offensive stat)
  4136. srl h
  4137. rr l
  4138. srl h
  4139. rr l
  4140. ld a, l
  4141. or h ; is the player's offensive stat 0?
  4142. jr nz, .next
  4143. inc l ; if the player's offensive stat is 0, bump it up to 1
  4144. .next
  4145. ld b, l ; b = player's offensive stat (possibly scaled)
  4146. ; (c already contains enemy's defensive stat (possibly scaled))
  4147. ld a, [wBattleMonLevel]
  4148. ld e, a ; e = level
  4149. ld a, [wCriticalHitOrOHKO]
  4150. and a ; check for critical hit
  4151. jr z, .done
  4152. sla e ; double level if it was a critical hit
  4153. .done
  4154. ld a, 1
  4155. and a
  4156. ret
  4157. ; sets b, c, d, and e for the CalculateDamage routine in the case of an attack by the enemy mon
  4158. GetDamageVarsForEnemyAttack:
  4159. ld hl, wDamage ; damage to eventually inflict, initialise to zero
  4160. xor a
  4161. ld [hli], a
  4162. ld [hl], a
  4163. ld hl, wEnemyMovePower
  4164. ld a, [hli]
  4165. ld d, a ; d = move power
  4166. and a
  4167. ret z ; return if move power is zero
  4168. ld a, [hl] ; a = [wEnemyMoveType]
  4169. cp FIRE ; types >= FIRE are all special
  4170. jr nc, .specialAttack
  4171. .physicalAttack
  4172. ld hl, wBattleMonDefense
  4173. ld a, [hli]
  4174. ld b, a
  4175. ld c, [hl] ; bc = player defense
  4176. ld a, [wPlayerBattleStatus3]
  4177. bit HAS_REFLECT_UP, a ; check for Reflect
  4178. jr z, .physicalAttackCritCheck
  4179. ; if the player has used Reflect, double the player's defense
  4180. sla c
  4181. rl b
  4182. .physicalAttackCritCheck
  4183. ld hl, wEnemyMonAttack
  4184. ld a, [wCriticalHitOrOHKO]
  4185. and a ; check for critical hit
  4186. jr z, .scaleStats
  4187. ; in the case of a critical hit, reset the player's defense and the enemy's attack to their base values
  4188. ld hl, wPartyMon1Defense
  4189. ld a, [wPlayerMonNumber]
  4190. ld bc, wPartyMon2 - wPartyMon1
  4191. call AddNTimes
  4192. ld a, [hli]
  4193. ld b, a
  4194. ld c, [hl]
  4195. push bc
  4196. ld c, 2 ; attack stat
  4197. call GetEnemyMonStat
  4198. ld hl, H_PRODUCT + 2
  4199. pop bc
  4200. jr .scaleStats
  4201. .specialAttack
  4202. ld hl, wBattleMonSpecial
  4203. ld a, [hli]
  4204. ld b, a
  4205. ld c, [hl]
  4206. ld a, [wPlayerBattleStatus3]
  4207. bit HAS_LIGHT_SCREEN_UP, a ; check for Light Screen
  4208. jr z, .specialAttackCritCheck
  4209. ; if the player has used Light Screen, double the player's special
  4210. sla c
  4211. rl b
  4212. ; reflect and light screen boosts do not cap the stat at 999, so weird things will happen during stats scaling if
  4213. ; a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
  4214. .specialAttackCritCheck
  4215. ld hl, wEnemyMonSpecial
  4216. ld a, [wCriticalHitOrOHKO]
  4217. and a ; check for critical hit
  4218. jr z, .scaleStats
  4219. ; in the case of a critical hit, reset the player's and enemy's specials to their base values
  4220. ld hl, wPartyMon1Special
  4221. ld a, [wPlayerMonNumber]
  4222. ld bc, wPartyMon2 - wPartyMon1
  4223. call AddNTimes
  4224. ld a, [hli]
  4225. ld b, a
  4226. ld c, [hl]
  4227. push bc
  4228. ld c, 5 ; special stat
  4229. call GetEnemyMonStat
  4230. ld hl, H_PRODUCT + 2
  4231. pop bc
  4232. ; if either the offensive or defensive stat is too large to store in a byte, scale both stats by dividing them by 4
  4233. ; this allows values with up to 10 bits (values up to 1023) to be handled
  4234. ; anything larger will wrap around
  4235. .scaleStats
  4236. ld a, [hli]
  4237. ld l, [hl]
  4238. ld h, a ; hl = enemy's offensive stat
  4239. or b ; is either high byte nonzero?
  4240. jr z, .next ; if not, we don't need to scale
  4241. ; bc /= 4 (scale player's defensive stat)
  4242. srl b
  4243. rr c
  4244. srl b
  4245. rr c
  4246. ; defensive stat can actually end up as 0, leading to a division by 0 freeze during damage calculation
  4247. ; hl /= 4 (scale enemy's offensive stat)
  4248. srl h
  4249. rr l
  4250. srl h
  4251. rr l
  4252. ld a, l
  4253. or h ; is the enemy's offensive stat 0?
  4254. jr nz, .next
  4255. inc l ; if the enemy's offensive stat is 0, bump it up to 1
  4256. .next
  4257. ld b, l ; b = enemy's offensive stat (possibly scaled)
  4258. ; (c already contains player's defensive stat (possibly scaled))
  4259. ld a, [wEnemyMonLevel]
  4260. ld e, a
  4261. ld a, [wCriticalHitOrOHKO]
  4262. and a ; check for critical hit
  4263. jr z, .done
  4264. sla e ; double level if it was a critical hit
  4265. .done
  4266. ld a, $1
  4267. and a
  4268. and a
  4269. ret
  4270. ; get stat c of enemy mon
  4271. ; c: stat to get (HP=1,Attack=2,Defense=3,Speed=4,Special=5)
  4272. GetEnemyMonStat:
  4273. push de
  4274. push bc
  4275. ld a, [wLinkState]
  4276. cp LINK_STATE_BATTLING
  4277. jr nz, .notLinkBattle
  4278. ld hl, wEnemyMon1Stats
  4279. dec c
  4280. sla c
  4281. ld b, $0
  4282. add hl, bc
  4283. ld a, [wEnemyMonPartyPos]
  4284. ld bc, wEnemyMon2 - wEnemyMon1
  4285. call AddNTimes
  4286. ld a, [hli]
  4287. ld [H_MULTIPLICAND + 1], a
  4288. ld a, [hl]
  4289. ld [H_MULTIPLICAND + 2], a
  4290. pop bc
  4291. pop de
  4292. ret
  4293. .notLinkBattle
  4294. ld a, [wEnemyMonLevel]
  4295. ld [wCurEnemyLVL], a
  4296. ld a, [wEnemyMonSpecies]
  4297. ld [wd0b5], a
  4298. call GetMonHeader
  4299. ld hl, wEnemyMonDVs
  4300. ld de, wLoadedMonSpeedExp
  4301. ld a, [hli]
  4302. ld [de], a
  4303. inc de
  4304. ld a, [hl]
  4305. ld [de], a
  4306. pop bc
  4307. ld b, $0
  4308. ld hl, wLoadedMonSpeedExp - $b ; this base address makes CalcStat look in [wLoadedMonSpeedExp] for DVs
  4309. call CalcStat
  4310. pop de
  4311. ret
  4312. CalculateDamage:
  4313. ; input:
  4314. ; b: attack
  4315. ; c: opponent defense
  4316. ; d: base power
  4317. ; e: level
  4318. ld a, [H_WHOSETURN] ; whose turn?
  4319. and a
  4320. ld a, [wPlayerMoveEffect]
  4321. jr z, .effect
  4322. ld a, [wEnemyMoveEffect]
  4323. .effect
  4324. ; EXPLODE_EFFECT halves defense.
  4325. cp EXPLODE_EFFECT
  4326. jr nz, .ok
  4327. srl c
  4328. jr nz, .ok
  4329. inc c ; ...with a minimum value of 1 (used as a divisor later on)
  4330. .ok
  4331. ; Multi-hit attacks may or may not have 0 bp.
  4332. cp TWO_TO_FIVE_ATTACKS_EFFECT
  4333. jr z, .skipbp
  4334. cp $1e
  4335. jr z, .skipbp
  4336. ; Calculate OHKO damage based on remaining HP.
  4337. cp OHKO_EFFECT
  4338. jp z, JumpToOHKOMoveEffect
  4339. ; Don't calculate damage for moves that don't do any.
  4340. ld a, d ; base power
  4341. and a
  4342. ret z
  4343. .skipbp
  4344. xor a
  4345. ld hl, H_DIVIDEND
  4346. ldi [hl], a
  4347. ldi [hl], a
  4348. ld [hl], a
  4349. ; Multiply level by 2
  4350. ld a, e ; level
  4351. add a
  4352. jr nc, .nc
  4353. push af
  4354. ld a, 1
  4355. ld [hl], a
  4356. pop af
  4357. .nc
  4358. inc hl
  4359. ldi [hl], a
  4360. ; Divide by 5
  4361. ld a, 5
  4362. ldd [hl], a
  4363. push bc
  4364. ld b, 4
  4365. call Divide
  4366. pop bc
  4367. ; Add 2
  4368. inc [hl]
  4369. inc [hl]
  4370. inc hl ; multiplier
  4371. ; Multiply by attack base power
  4372. ld [hl], d
  4373. call Multiply
  4374. ; Multiply by attack stat
  4375. ld [hl], b
  4376. call Multiply
  4377. ; Divide by defender's defense stat
  4378. ld [hl], c
  4379. ld b, 4
  4380. call Divide
  4381. ; Divide by 50
  4382. ld [hl], 50
  4383. ld b, 4
  4384. call Divide
  4385. ld hl, wDamage
  4386. ld b, [hl]
  4387. ld a, [H_QUOTIENT + 3]
  4388. add b
  4389. ld [H_QUOTIENT + 3], a
  4390. jr nc, .asm_3dfd0
  4391. ld a, [H_QUOTIENT + 2]
  4392. inc a
  4393. ld [H_QUOTIENT + 2], a
  4394. and a
  4395. jr z, .asm_3e004
  4396. .asm_3dfd0
  4397. ld a, [H_QUOTIENT]
  4398. ld b, a
  4399. ld a, [H_QUOTIENT + 1]
  4400. or a
  4401. jr nz, .asm_3e004
  4402. ld a, [H_QUOTIENT + 2]
  4403. cp 998 / $100
  4404. jr c, .asm_3dfe8
  4405. cp 998 / $100 + 1
  4406. jr nc, .asm_3e004
  4407. ld a, [H_QUOTIENT + 3]
  4408. cp 998 % $100
  4409. jr nc, .asm_3e004
  4410. .asm_3dfe8
  4411. inc hl
  4412. ld a, [H_QUOTIENT + 3]
  4413. ld b, [hl]
  4414. add b
  4415. ld [hld], a
  4416. ld a, [H_QUOTIENT + 2]
  4417. ld b, [hl]
  4418. adc b
  4419. ld [hl], a
  4420. jr c, .asm_3e004
  4421. ld a, [hl]
  4422. cp 998 / $100
  4423. jr c, .asm_3e00a
  4424. cp 998 / $100 + 1
  4425. jr nc, .asm_3e004
  4426. inc hl
  4427. ld a, [hld]
  4428. cp 998 % $100
  4429. jr c, .asm_3e00a
  4430. .asm_3e004
  4431. ; cap at 997
  4432. ld a, 997 / $100
  4433. ld [hli], a
  4434. ld a, 997 % $100
  4435. ld [hld], a
  4436. .asm_3e00a
  4437. ; add 2
  4438. inc hl
  4439. ld a, [hl]
  4440. add 2
  4441. ld [hld], a
  4442. jr nc, .done
  4443. inc [hl]
  4444. .done
  4445. ; minimum damage is 1
  4446. ld a, 1
  4447. and a
  4448. ret
  4449. JumpToOHKOMoveEffect:
  4450. call JumpMoveEffect
  4451. ld a, [wMoveMissed]
  4452. dec a
  4453. ret
  4454. UnusedHighCriticalMoves:
  4455. db KARATE_CHOP
  4456. db RAZOR_LEAF
  4457. db CRABHAMMER
  4458. db SLASH
  4459. db $FF
  4460. ; determines if attack is a critical hit
  4461. ; azure heights claims "the fastest pokémon (who are,not coincidentally,
  4462. ; among the most popular) tend to CH about 20 to 25% of the time."
  4463. CriticalHitTest:
  4464. xor a
  4465. ld [wCriticalHitOrOHKO], a
  4466. ld a, [H_WHOSETURN]
  4467. and a
  4468. ld a, [wEnemyMonSpecies]
  4469. jr nz, .handleEnemy
  4470. ld a, [wBattleMonSpecies]
  4471. .handleEnemy
  4472. ld [wd0b5], a
  4473. call GetMonHeader
  4474. ld a, [wMonHBaseSpeed]
  4475. ld b, a
  4476. srl b ; (effective (base speed/2))
  4477. ld a, [H_WHOSETURN]
  4478. and a
  4479. ld hl, wPlayerMovePower
  4480. ld de, wPlayerBattleStatus2
  4481. jr z, .calcCriticalHitProbability
  4482. ld hl, wEnemyMovePower
  4483. ld de, wEnemyBattleStatus2
  4484. .calcCriticalHitProbability
  4485. ld a, [hld] ; read base power from RAM
  4486. and a
  4487. ret z ; do nothing if zero
  4488. dec hl
  4489. ld c, [hl] ; read move id
  4490. ld a, [de]
  4491. bit GETTING_PUMPED, a ; test for focus energy
  4492. jr nz, .focusEnergyUsed ; bug: using focus energy causes a shift to the right instead of left,
  4493. ; resulting in 1/4 the usual crit chance
  4494. sla b ; (effective (base speed/2)*2)
  4495. jr nc, .noFocusEnergyUsed
  4496. ld b, $ff ; cap at 255/256
  4497. jr .noFocusEnergyUsed
  4498. .focusEnergyUsed
  4499. srl b
  4500. .noFocusEnergyUsed
  4501. ld hl, HighCriticalMoves ; table of high critical hit moves
  4502. .Loop
  4503. ld a, [hli] ; read move from move table
  4504. cp c ; does it match the move about to be used?
  4505. jr z, .HighCritical ; if so, the move about to be used is a high critical hit ratio move
  4506. inc a ; move on to the next move, FF terminates loop
  4507. jr nz, .Loop ; check the next move in HighCriticalMoves
  4508. srl b ; /2 for regular move (effective (base speed / 2))
  4509. jr .SkipHighCritical ; continue as a normal move
  4510. .HighCritical
  4511. sla b ; *2 for high critical hit moves
  4512. jr nc, .noCarry
  4513. ld b, $ff ; cap at 255/256
  4514. .noCarry
  4515. sla b ; *4 for high critical move (effective (base speed/2)*8))
  4516. jr nc, .SkipHighCritical
  4517. ld b, $ff
  4518. .SkipHighCritical
  4519. call BattleRandom ; generates a random value, in "a"
  4520. rlc a
  4521. rlc a
  4522. rlc a
  4523. cp b ; check a against calculated crit rate
  4524. ret nc ; no critical hit if no borrow
  4525. ld a, $1
  4526. ld [wCriticalHitOrOHKO], a ; set critical hit flag
  4527. ret
  4528. ; high critical hit moves
  4529. HighCriticalMoves:
  4530. db KARATE_CHOP
  4531. db RAZOR_LEAF
  4532. db CRABHAMMER
  4533. db SLASH
  4534. db $FF
  4535. ; function to determine if Counter hits and if so, how much damage it does
  4536. HandleCounterMove:
  4537. ; The variables checked by Counter are updated whenever the cursor points to a new move in the battle selection menu.
  4538. ; This is irrelevant for the opponent's side outside of link battles, since the move selection is controlled by the AI.
  4539. ; However, in the scenario where the player switches out and the opponent uses Counter,
  4540. ; the outcome may be affected by the player's actions in the move selection menu prior to switching the Pokemon.
  4541. ; This might also lead to desync glitches in link battles.
  4542. ld a, [H_WHOSETURN] ; whose turn
  4543. and a
  4544. ; player's turn
  4545. ld hl, wEnemySelectedMove
  4546. ld de, wEnemyMovePower
  4547. ld a, [wPlayerSelectedMove]
  4548. jr z, .next
  4549. ; enemy's turn
  4550. ld hl, wPlayerSelectedMove
  4551. ld de, wPlayerMovePower
  4552. ld a, [wEnemySelectedMove]
  4553. .next
  4554. cp COUNTER
  4555. ret nz ; return if not using Counter
  4556. ld a, $01
  4557. ld [wMoveMissed], a ; initialize the move missed variable to true (it is set to false below if the move hits)
  4558. ld a, [hl]
  4559. cp COUNTER
  4560. ret z ; miss if the opponent's last selected move is Counter.
  4561. ld a, [de]
  4562. and a
  4563. ret z ; miss if the opponent's last selected move's Base Power is 0.
  4564. ; check if the move the target last selected was Normal or Fighting type
  4565. inc de
  4566. ld a, [de]
  4567. and a ; normal type
  4568. jr z, .counterableType
  4569. cp FIGHTING
  4570. jr z, .counterableType
  4571. ; if the move wasn't Normal or Fighting type, miss
  4572. xor a
  4573. ret
  4574. .counterableType
  4575. ld hl, wDamage
  4576. ld a, [hli]
  4577. or [hl]
  4578. ret z ; If we made it here, Counter still misses if the last move used in battle did no damage to its target.
  4579. ; wDamage is shared by both players, so Counter may strike back damage dealt by the Counter user itself
  4580. ; if the conditions meet, even though 99% of the times damage will come from the target.
  4581. ; if it did damage, double it
  4582. ld a, [hl]
  4583. add a
  4584. ldd [hl], a
  4585. ld a, [hl]
  4586. adc a
  4587. ld [hl], a
  4588. jr nc, .noCarry
  4589. ; damage is capped at 0xFFFF
  4590. ld a, $ff
  4591. ld [hli], a
  4592. ld [hl], a
  4593. .noCarry
  4594. xor a
  4595. ld [wMoveMissed], a
  4596. call MoveHitTest ; do the normal move hit test in addition to Counter's special rules
  4597. xor a
  4598. ret
  4599. ApplyAttackToEnemyPokemon:
  4600. ld a, [wPlayerMoveEffect]
  4601. cp OHKO_EFFECT
  4602. jr z, ApplyDamageToEnemyPokemon
  4603. cp SUPER_FANG_EFFECT
  4604. jr z, .superFangEffect
  4605. cp SPECIAL_DAMAGE_EFFECT
  4606. jr z, .specialDamage
  4607. ld a, [wPlayerMovePower]
  4608. and a
  4609. jp z, ApplyAttackToEnemyPokemonDone ; no attack to apply if base power is 0
  4610. jr ApplyDamageToEnemyPokemon
  4611. .superFangEffect
  4612. ; set the damage to half the target's HP
  4613. ld hl, wEnemyMonHP
  4614. ld de, wDamage
  4615. ld a, [hli]
  4616. srl a
  4617. ld [de], a
  4618. inc de
  4619. ld b, a
  4620. ld a, [hl]
  4621. rr a
  4622. ld [de], a
  4623. or b
  4624. jr nz, ApplyDamageToEnemyPokemon
  4625. ; make sure Super Fang's damage is always at least 1
  4626. ld a, $01
  4627. ld [de], a
  4628. jr ApplyDamageToEnemyPokemon
  4629. .specialDamage
  4630. ld hl, wBattleMonLevel
  4631. ld a, [hl]
  4632. ld b, a ; Seismic Toss deals damage equal to the user's level
  4633. ld a, [wPlayerMoveNum]
  4634. cp SEISMIC_TOSS
  4635. jr z, .storeDamage
  4636. cp NIGHT_SHADE
  4637. jr z, .storeDamage
  4638. ld b, SONICBOOM_DAMAGE ; 20
  4639. cp SONICBOOM
  4640. jr z, .storeDamage
  4641. ld b, DRAGON_RAGE_DAMAGE ; 40
  4642. cp DRAGON_RAGE
  4643. jr z, .storeDamage
  4644. ; Psywave
  4645. ld a, [hl]
  4646. ld b, a
  4647. srl a
  4648. add b
  4649. ld b, a ; b = level * 1.5
  4650. ; loop until a random number in the range [1, b) is found
  4651. .loop
  4652. call BattleRandom
  4653. and a
  4654. jr z, .loop
  4655. cp b
  4656. jr nc, .loop
  4657. ld b, a
  4658. .storeDamage ; store damage value at b
  4659. ld hl, wDamage
  4660. xor a
  4661. ld [hli], a
  4662. ld a, b
  4663. ld [hl], a
  4664. ApplyDamageToEnemyPokemon:
  4665. ld hl, wDamage
  4666. ld a, [hli]
  4667. ld b, a
  4668. ld a, [hl]
  4669. or b
  4670. jr z, ApplyAttackToEnemyPokemonDone ; we're done if damage is 0
  4671. ld a, [wEnemyBattleStatus2]
  4672. bit HAS_SUBSTITUTE_UP, a ; does the enemy have a substitute?
  4673. jp nz, AttackSubstitute
  4674. ; subtract the damage from the pokemon's current HP
  4675. ; also, save the current HP at wHPBarOldHP
  4676. ld a, [hld]
  4677. ld b, a
  4678. ld a, [wEnemyMonHP + 1]
  4679. ld [wHPBarOldHP], a
  4680. sub b
  4681. ld [wEnemyMonHP + 1], a
  4682. ld a, [hl]
  4683. ld b, a
  4684. ld a, [wEnemyMonHP]
  4685. ld [wHPBarOldHP+1], a
  4686. sbc b
  4687. ld [wEnemyMonHP], a
  4688. jr nc, .animateHpBar
  4689. ; if more damage was done than the current HP, zero the HP and set the damage (wDamage)
  4690. ; equal to how much HP the pokemon had before the attack
  4691. ld a, [wHPBarOldHP+1]
  4692. ld [hli], a
  4693. ld a, [wHPBarOldHP]
  4694. ld [hl], a
  4695. xor a
  4696. ld hl, wEnemyMonHP
  4697. ld [hli], a
  4698. ld [hl], a
  4699. .animateHpBar
  4700. ld hl, wEnemyMonMaxHP
  4701. ld a, [hli]
  4702. ld [wHPBarMaxHP+1], a
  4703. ld a, [hl]
  4704. ld [wHPBarMaxHP], a
  4705. ld hl, wEnemyMonHP
  4706. ld a, [hli]
  4707. ld [wHPBarNewHP+1], a
  4708. ld a, [hl]
  4709. ld [wHPBarNewHP], a
  4710. coord hl, 2, 2
  4711. xor a
  4712. ld [wHPBarType], a
  4713. predef UpdateHPBar2 ; animate the HP bar shortening
  4714. ApplyAttackToEnemyPokemonDone:
  4715. jp DrawHUDsAndHPBars
  4716. ApplyAttackToPlayerPokemon:
  4717. ld a, [wEnemyMoveEffect]
  4718. cp OHKO_EFFECT
  4719. jr z, ApplyDamageToPlayerPokemon
  4720. cp SUPER_FANG_EFFECT
  4721. jr z, .superFangEffect
  4722. cp SPECIAL_DAMAGE_EFFECT
  4723. jr z, .specialDamage
  4724. ld a, [wEnemyMovePower]
  4725. and a
  4726. jp z, ApplyAttackToPlayerPokemonDone
  4727. jr ApplyDamageToPlayerPokemon
  4728. .superFangEffect
  4729. ; set the damage to half the target's HP
  4730. ld hl, wBattleMonHP
  4731. ld de, wDamage
  4732. ld a, [hli]
  4733. srl a
  4734. ld [de], a
  4735. inc de
  4736. ld b, a
  4737. ld a, [hl]
  4738. rr a
  4739. ld [de], a
  4740. or b
  4741. jr nz, ApplyDamageToPlayerPokemon
  4742. ; make sure Super Fang's damage is always at least 1
  4743. ld a, $01
  4744. ld [de], a
  4745. jr ApplyDamageToPlayerPokemon
  4746. .specialDamage
  4747. ld hl, wEnemyMonLevel
  4748. ld a, [hl]
  4749. ld b, a
  4750. ld a, [wEnemyMoveNum]
  4751. cp SEISMIC_TOSS
  4752. jr z, .storeDamage
  4753. cp NIGHT_SHADE
  4754. jr z, .storeDamage
  4755. ld b, SONICBOOM_DAMAGE
  4756. cp SONICBOOM
  4757. jr z, .storeDamage
  4758. ld b, DRAGON_RAGE_DAMAGE
  4759. cp DRAGON_RAGE
  4760. jr z, .storeDamage
  4761. ; Psywave
  4762. ld a, [hl]
  4763. ld b, a
  4764. srl a
  4765. add b
  4766. ld b, a ; b = attacker's level * 1.5
  4767. ; loop until a random number in the range [0, b) is found
  4768. ; this differs from the range when the player attacks, which is [1, b)
  4769. ; it's possible for the enemy to do 0 damage with Psywave, but the player always does at least 1 damage
  4770. .loop
  4771. call BattleRandom
  4772. cp b
  4773. jr nc, .loop
  4774. ld b, a
  4775. .storeDamage
  4776. ld hl, wDamage
  4777. xor a
  4778. ld [hli], a
  4779. ld a, b
  4780. ld [hl], a
  4781. ApplyDamageToPlayerPokemon:
  4782. ld hl, wDamage
  4783. ld a, [hli]
  4784. ld b, a
  4785. ld a, [hl]
  4786. or b
  4787. jr z, ApplyAttackToPlayerPokemonDone ; we're done if damage is 0
  4788. ld a, [wPlayerBattleStatus2]
  4789. bit HAS_SUBSTITUTE_UP, a ; does the player have a substitute?
  4790. jp nz, AttackSubstitute
  4791. ; subtract the damage from the pokemon's current HP
  4792. ; also, save the current HP at wHPBarOldHP and the new HP at wHPBarNewHP
  4793. ld a, [hld]
  4794. ld b, a
  4795. ld a, [wBattleMonHP + 1]
  4796. ld [wHPBarOldHP], a
  4797. sub b
  4798. ld [wBattleMonHP + 1], a
  4799. ld [wHPBarNewHP], a
  4800. ld b, [hl]
  4801. ld a, [wBattleMonHP]
  4802. ld [wHPBarOldHP+1], a
  4803. sbc b
  4804. ld [wBattleMonHP], a
  4805. ld [wHPBarNewHP+1], a
  4806. jr nc, .animateHpBar
  4807. ; if more damage was done than the current HP, zero the HP and set the damage (wDamage)
  4808. ; equal to how much HP the pokemon had before the attack
  4809. ld a, [wHPBarOldHP+1]
  4810. ld [hli], a
  4811. ld a, [wHPBarOldHP]
  4812. ld [hl], a
  4813. xor a
  4814. ld hl, wBattleMonHP
  4815. ld [hli], a
  4816. ld [hl], a
  4817. ld hl, wHPBarNewHP
  4818. ld [hli], a
  4819. ld [hl], a
  4820. .animateHpBar
  4821. ld hl, wBattleMonMaxHP
  4822. ld a, [hli]
  4823. ld [wHPBarMaxHP+1], a
  4824. ld a, [hl]
  4825. ld [wHPBarMaxHP], a
  4826. coord hl, 10, 9
  4827. ld a, $01
  4828. ld [wHPBarType], a
  4829. predef UpdateHPBar2 ; animate the HP bar shortening
  4830. ApplyAttackToPlayerPokemonDone:
  4831. jp DrawHUDsAndHPBars
  4832. AttackSubstitute:
  4833. ; Unlike the two ApplyAttackToPokemon functions, Attack Substitute is shared by player and enemy.
  4834. ; Self-confusion damage as well as Hi-Jump Kick and Jump Kick recoil cause a momentary turn swap before being applied.
  4835. ; If the user has a Substitute up and would take damage because of that,
  4836. ; damage will be applied to the other player's Substitute.
  4837. ; Normal recoil such as from Double-Edge isn't affected by this glitch,
  4838. ; because this function is never called in that case.
  4839. ld hl, SubstituteTookDamageText
  4840. call PrintText
  4841. ; values for player turn
  4842. ld de, wEnemySubstituteHP
  4843. ld bc, wEnemyBattleStatus2
  4844. ld a, [H_WHOSETURN]
  4845. and a
  4846. jr z, .applyDamageToSubstitute
  4847. ; values for enemy turn
  4848. ld de, wPlayerSubstituteHP
  4849. ld bc, wPlayerBattleStatus2
  4850. .applyDamageToSubstitute
  4851. ld hl, wDamage
  4852. ld a, [hli]
  4853. and a
  4854. jr nz, .substituteBroke ; damage > 0xFF always breaks substitutes
  4855. ; subtract damage from HP of substitute
  4856. ld a, [de]
  4857. sub [hl]
  4858. ld [de], a
  4859. ret nc
  4860. .substituteBroke
  4861. ; If the target's Substitute breaks, wDamage isn't updated with the amount of HP
  4862. ; the Substitute had before being attacked.
  4863. ld h, b
  4864. ld l, c
  4865. res HAS_SUBSTITUTE_UP, [hl] ; unset the substitute bit
  4866. ld hl, SubstituteBrokeText
  4867. call PrintText
  4868. ; flip whose turn it is for the next function call
  4869. ld a, [H_WHOSETURN]
  4870. xor $01
  4871. ld [H_WHOSETURN], a
  4872. callab HideSubstituteShowMonAnim ; animate the substitute breaking
  4873. ; flip the turn back to the way it was
  4874. ld a, [H_WHOSETURN]
  4875. xor $01
  4876. ld [H_WHOSETURN], a
  4877. ld hl, wPlayerMoveEffect ; value for player's turn
  4878. and a
  4879. jr z, .nullifyEffect
  4880. ld hl, wEnemyMoveEffect ; value for enemy's turn
  4881. .nullifyEffect
  4882. xor a
  4883. ld [hl], a ; zero the effect of the attacker's move
  4884. jp DrawHUDsAndHPBars
  4885. SubstituteTookDamageText:
  4886. TX_FAR _SubstituteTookDamageText
  4887. db "@"
  4888. SubstituteBrokeText:
  4889. TX_FAR _SubstituteBrokeText
  4890. db "@"
  4891. ; this function raises the attack modifier of a pokemon using Rage when that pokemon is attacked
  4892. HandleBuildingRage:
  4893. ; values for the player turn
  4894. ld hl, wEnemyBattleStatus2
  4895. ld de, wEnemyMonStatMods
  4896. ld bc, wEnemyMoveNum
  4897. ld a, [H_WHOSETURN]
  4898. and a
  4899. jr z, .next
  4900. ; values for the enemy turn
  4901. ld hl, wPlayerBattleStatus2
  4902. ld de, wPlayerMonStatMods
  4903. ld bc, wPlayerMoveNum
  4904. .next
  4905. bit USING_RAGE, [hl] ; is the pokemon being attacked under the effect of Rage?
  4906. ret z ; return if not
  4907. ld a, [de]
  4908. cp $0d ; maximum stat modifier value
  4909. ret z ; return if attack modifier is already maxed
  4910. ld a, [H_WHOSETURN]
  4911. xor $01 ; flip turn for the stat modifier raising function
  4912. ld [H_WHOSETURN], a
  4913. ; temporarily change the target pokemon's move to $00 and the effect to the one
  4914. ; that causes the attack modifier to go up one stage
  4915. ld h, b
  4916. ld l, c
  4917. ld [hl], $00 ; null move number
  4918. inc hl
  4919. ld [hl], ATTACK_UP1_EFFECT
  4920. push hl
  4921. ld hl, BuildingRageText
  4922. call PrintText
  4923. call StatModifierUpEffect ; stat modifier raising function
  4924. pop hl
  4925. xor a
  4926. ldd [hl], a ; null move effect
  4927. ld a, RAGE
  4928. ld [hl], a ; restore the target pokemon's move number to Rage
  4929. ld a, [H_WHOSETURN]
  4930. xor $01 ; flip turn back to the way it was
  4931. ld [H_WHOSETURN], a
  4932. ret
  4933. BuildingRageText:
  4934. TX_FAR _BuildingRageText
  4935. db "@"
  4936. ; copy last move for Mirror Move
  4937. ; sets zero flag on failure and unsets zero flag on success
  4938. MirrorMoveCopyMove:
  4939. ; Mirror Move makes use of ccf1 (wPlayerUsedMove) and ccf2 (wEnemyUsedMove) addresses,
  4940. ; which are mainly used to print the "[Pokemon] used [Move]" text.
  4941. ; Both are set to 0 whenever a new Pokemon is sent out
  4942. ; ccf1 is also set to 0 whenever the player is fast asleep or frozen solid.
  4943. ; ccf2 is also set to 0 whenever the enemy is fast asleep or frozen solid.
  4944. ld a, [H_WHOSETURN]
  4945. and a
  4946. ; values for player turn
  4947. ld a, [wEnemyUsedMove]
  4948. ld hl, wPlayerSelectedMove
  4949. ld de, wPlayerMoveNum
  4950. jr z, .next
  4951. ; values for enemy turn
  4952. ld a, [wPlayerUsedMove]
  4953. ld de, wEnemyMoveNum
  4954. ld hl, wEnemySelectedMove
  4955. .next
  4956. ld [hl], a
  4957. cp MIRROR_MOVE ; did the target Pokemon last use Mirror Move, and miss?
  4958. jr z, .mirrorMoveFailed
  4959. and a ; has the target selected any move yet?
  4960. jr nz, ReloadMoveData
  4961. .mirrorMoveFailed
  4962. ld hl, MirrorMoveFailedText
  4963. call PrintText
  4964. xor a
  4965. ret
  4966. MirrorMoveFailedText:
  4967. TX_FAR _MirrorMoveFailedText
  4968. db "@"
  4969. ; function used to reload move data for moves like Mirror Move and Metronome
  4970. ReloadMoveData:
  4971. ld [wd11e], a
  4972. dec a
  4973. ld hl, Moves
  4974. ld bc, MoveEnd - Moves
  4975. call AddNTimes
  4976. ld a, BANK(Moves)
  4977. call FarCopyData ; copy the move's stats
  4978. call IncrementMovePP
  4979. ; the follow two function calls are used to reload the move name
  4980. call GetMoveName
  4981. call CopyStringToCF4B
  4982. ld a, $01
  4983. and a
  4984. ret
  4985. ; function that picks a random move for metronome
  4986. MetronomePickMove:
  4987. xor a
  4988. ld [wAnimationType], a
  4989. ld a, METRONOME
  4990. call PlayMoveAnimation ; play Metronome's animation
  4991. ; values for player turn
  4992. ld de, wPlayerMoveNum
  4993. ld hl, wPlayerSelectedMove
  4994. ld a, [H_WHOSETURN]
  4995. and a
  4996. jr z, .pickMoveLoop
  4997. ; values for enemy turn
  4998. ld de, wEnemyMoveNum
  4999. ld hl, wEnemySelectedMove
  5000. ; loop to pick a random number in the range [1, $a5) to be the move used by Metronome
  5001. .pickMoveLoop
  5002. call BattleRandom
  5003. and a
  5004. jr z, .pickMoveLoop
  5005. cp NUM_ATTACKS + 1 ; max normal move number + 1 (this is Struggle's move number)
  5006. jr nc, .pickMoveLoop
  5007. cp METRONOME
  5008. jr z, .pickMoveLoop
  5009. ld [hl], a
  5010. jr ReloadMoveData
  5011. ; this function increments the current move's PP
  5012. ; it's used to prevent moves that run another move within the same turn
  5013. ; (like Mirror Move and Metronome) from losing 2 PP
  5014. IncrementMovePP:
  5015. ld a, [H_WHOSETURN]
  5016. and a
  5017. ; values for player turn
  5018. ld hl, wBattleMonPP
  5019. ld de, wPartyMon1PP
  5020. ld a, [wPlayerMoveListIndex]
  5021. jr z, .next
  5022. ; values for enemy turn
  5023. ld hl, wEnemyMonPP
  5024. ld de, wEnemyMon1PP
  5025. ld a, [wEnemyMoveListIndex]
  5026. .next
  5027. ld b, $00
  5028. ld c, a
  5029. add hl, bc
  5030. inc [hl] ; increment PP in the currently battling pokemon memory location
  5031. ld h, d
  5032. ld l, e
  5033. add hl, bc
  5034. ld a, [H_WHOSETURN]
  5035. and a
  5036. ld a, [wPlayerMonNumber] ; value for player turn
  5037. jr z, .updatePP
  5038. ld a, [wEnemyMonPartyPos] ; value for enemy turn
  5039. .updatePP
  5040. ld bc, wEnemyMon2 - wEnemyMon1
  5041. call AddNTimes
  5042. inc [hl] ; increment PP in the party memory location
  5043. ret
  5044. ; function to adjust the base damage of an attack to account for type effectiveness
  5045. AdjustDamageForMoveType:
  5046. ; values for player turn
  5047. ld hl, wBattleMonType
  5048. ld a, [hli]
  5049. ld b, a ; b = type 1 of attacker
  5050. ld c, [hl] ; c = type 2 of attacker
  5051. ld hl, wEnemyMonType
  5052. ld a, [hli]
  5053. ld d, a ; d = type 1 of defender
  5054. ld e, [hl] ; e = type 2 of defender
  5055. ld a, [wPlayerMoveType]
  5056. ld [wMoveType], a
  5057. ld a, [H_WHOSETURN]
  5058. and a
  5059. jr z, .next
  5060. ; values for enemy turn
  5061. ld hl, wEnemyMonType
  5062. ld a, [hli]
  5063. ld b, a ; b = type 1 of attacker
  5064. ld c, [hl] ; c = type 2 of attacker
  5065. ld hl, wBattleMonType
  5066. ld a, [hli]
  5067. ld d, a ; d = type 1 of defender
  5068. ld e, [hl] ; e = type 2 of defender
  5069. ld a, [wEnemyMoveType]
  5070. ld [wMoveType], a
  5071. .next
  5072. ld a, [wMoveType]
  5073. cp b ; does the move type match type 1 of the attacker?
  5074. jr z, .sameTypeAttackBonus
  5075. cp c ; does the move type match type 2 of the attacker?
  5076. jr z, .sameTypeAttackBonus
  5077. jr .skipSameTypeAttackBonus
  5078. .sameTypeAttackBonus
  5079. ; if the move type matches one of the attacker's types
  5080. ld hl, wDamage + 1
  5081. ld a, [hld]
  5082. ld h, [hl]
  5083. ld l, a ; hl = damage
  5084. ld b, h
  5085. ld c, l ; bc = damage
  5086. srl b
  5087. rr c ; bc = floor(0.5 * damage)
  5088. add hl, bc ; hl = floor(1.5 * damage)
  5089. ; store damage
  5090. ld a, h
  5091. ld [wDamage], a
  5092. ld a, l
  5093. ld [wDamage + 1], a
  5094. ld hl, wDamageMultipliers
  5095. set 7, [hl]
  5096. .skipSameTypeAttackBonus
  5097. ld a, [wMoveType]
  5098. ld b, a
  5099. ld hl, TypeEffects
  5100. .loop
  5101. ld a, [hli] ; a = "attacking type" of the current type pair
  5102. cp $ff
  5103. jr z, .done
  5104. cp b ; does move type match "attacking type"?
  5105. jr nz, .nextTypePair
  5106. ld a, [hl] ; a = "defending type" of the current type pair
  5107. cp d ; does type 1 of defender match "defending type"?
  5108. jr z, .matchingPairFound
  5109. cp e ; does type 2 of defender match "defending type"?
  5110. jr z, .matchingPairFound
  5111. jr .nextTypePair
  5112. .matchingPairFound
  5113. ; if the move type matches the "attacking type" and one of the defender's types matches the "defending type"
  5114. push hl
  5115. push bc
  5116. inc hl
  5117. ld a, [wDamageMultipliers]
  5118. and $80
  5119. ld b, a
  5120. ld a, [hl] ; a = damage multiplier
  5121. ld [H_MULTIPLIER], a
  5122. add b
  5123. ld [wDamageMultipliers], a
  5124. xor a
  5125. ld [H_MULTIPLICAND], a
  5126. ld hl, wDamage
  5127. ld a, [hli]
  5128. ld [H_MULTIPLICAND + 1], a
  5129. ld a, [hld]
  5130. ld [H_MULTIPLICAND + 2], a
  5131. call Multiply
  5132. ld a, 10
  5133. ld [H_DIVISOR], a
  5134. ld b, $04
  5135. call Divide
  5136. ld a, [H_QUOTIENT + 2]
  5137. ld [hli], a
  5138. ld b, a
  5139. ld a, [H_QUOTIENT + 3]
  5140. ld [hl], a
  5141. or b ; is damage 0?
  5142. jr nz, .skipTypeImmunity
  5143. .typeImmunity
  5144. ; if damage is 0, make the move miss
  5145. ; this only occurs if a move that would do 2 or 3 damage is 0.25x effective against the target
  5146. inc a
  5147. ld [wMoveMissed], a
  5148. .skipTypeImmunity
  5149. pop bc
  5150. pop hl
  5151. .nextTypePair
  5152. inc hl
  5153. inc hl
  5154. jp .loop
  5155. .done
  5156. ret
  5157. ; function to tell how effective the type of an enemy attack is on the player's current pokemon
  5158. ; this doesn't take into account the effects that dual types can have
  5159. ; (e.g. 4x weakness / resistance, weaknesses and resistances canceling)
  5160. ; the result is stored in [wTypeEffectiveness]
  5161. ; ($05 is not very effective, $10 is neutral, $14 is super effective)
  5162. ; as far is can tell, this is only used once in some AI code to help decide which move to use
  5163. AIGetTypeEffectiveness:
  5164. ld a, [wEnemyMoveType]
  5165. ld d, a ; d = type of enemy move
  5166. ld hl, wBattleMonType
  5167. ld b, [hl] ; b = type 1 of player's pokemon
  5168. inc hl
  5169. ld c, [hl] ; c = type 2 of player's pokemon
  5170. ld a, $10
  5171. ld [wTypeEffectiveness], a ; initialize to neutral effectiveness
  5172. ld hl, TypeEffects
  5173. .loop
  5174. ld a, [hli]
  5175. cp $ff
  5176. ret z
  5177. cp d ; match the type of the move
  5178. jr nz, .nextTypePair1
  5179. ld a, [hli]
  5180. cp b ; match with type 1 of pokemon
  5181. jr z, .done
  5182. cp c ; or match with type 2 of pokemon
  5183. jr z, .done
  5184. jr .nextTypePair2
  5185. .nextTypePair1
  5186. inc hl
  5187. .nextTypePair2
  5188. inc hl
  5189. jr .loop
  5190. .done
  5191. ld a, [hl]
  5192. ld [wTypeEffectiveness], a ; store damage multiplier
  5193. ret
  5194. INCLUDE "data/type_effects.asm"
  5195. ; some tests that need to pass for a move to hit
  5196. MoveHitTest:
  5197. ; player's turn
  5198. ld hl, wEnemyBattleStatus1
  5199. ld de, wPlayerMoveEffect
  5200. ld bc, wEnemyMonStatus
  5201. ld a, [H_WHOSETURN]
  5202. and a
  5203. jr z, .dreamEaterCheck
  5204. ; enemy's turn
  5205. ld hl, wPlayerBattleStatus1
  5206. ld de, wEnemyMoveEffect
  5207. ld bc, wBattleMonStatus
  5208. .dreamEaterCheck
  5209. ld a, [de]
  5210. cp DREAM_EATER_EFFECT
  5211. jr nz, .swiftCheck
  5212. ld a, [bc]
  5213. and SLP ; is the target pokemon sleeping?
  5214. jp z, .moveMissed
  5215. .swiftCheck
  5216. ld a, [de]
  5217. cp SWIFT_EFFECT
  5218. ret z ; Swift never misses (interestingly, Azure Heights lists this is a myth, but it appears to be true)
  5219. call CheckTargetSubstitute ; substitute check (note that this overwrites a)
  5220. jr z, .checkForDigOrFlyStatus
  5221. ; this code is buggy. it's supposed to prevent HP draining moves from working on substitutes.
  5222. ; since $7b79 overwrites a with either $00 or $01, it never works.
  5223. cp DRAIN_HP_EFFECT
  5224. jp z, .moveMissed
  5225. cp DREAM_EATER_EFFECT
  5226. jp z, .moveMissed
  5227. .checkForDigOrFlyStatus
  5228. bit INVULNERABLE, [hl]
  5229. jp nz, .moveMissed
  5230. ld a, [H_WHOSETURN]
  5231. and a
  5232. jr nz, .enemyTurn
  5233. .playerTurn
  5234. ; this checks if the move effect is disallowed by mist
  5235. ld a, [wPlayerMoveEffect]
  5236. cp ATTACK_DOWN1_EFFECT
  5237. jr c, .skipEnemyMistCheck
  5238. cp HAZE_EFFECT + 1
  5239. jr c, .enemyMistCheck
  5240. cp ATTACK_DOWN2_EFFECT
  5241. jr c, .skipEnemyMistCheck
  5242. cp REFLECT_EFFECT + 1
  5243. jr c, .enemyMistCheck
  5244. jr .skipEnemyMistCheck
  5245. .enemyMistCheck
  5246. ; if move effect is from $12 to $19 inclusive or $3a to $41 inclusive
  5247. ; i.e. the following moves
  5248. ; GROWL, TAIL WHIP, LEER, STRING SHOT, SAND-ATTACK, SMOKESCREEN, KINESIS,
  5249. ; FLASH, CONVERSION*, HAZE*, SCREECH, LIGHT SCREEN*, REFLECT*
  5250. ; the moves that are marked with an asterisk are not affected since this
  5251. ; function is not called when those moves are used
  5252. ld a, [wEnemyBattleStatus2]
  5253. bit PROTECTED_BY_MIST, a ; is mon protected by mist?
  5254. jp nz, .moveMissed
  5255. .skipEnemyMistCheck
  5256. ld a, [wPlayerBattleStatus2]
  5257. bit USING_X_ACCURACY, a ; is the player using X Accuracy?
  5258. ret nz ; if so, always hit regardless of accuracy/evasion
  5259. jr .calcHitChance
  5260. .enemyTurn
  5261. ld a, [wEnemyMoveEffect]
  5262. cp ATTACK_DOWN1_EFFECT
  5263. jr c, .skipPlayerMistCheck
  5264. cp HAZE_EFFECT + 1
  5265. jr c, .playerMistCheck
  5266. cp ATTACK_DOWN2_EFFECT
  5267. jr c, .skipPlayerMistCheck
  5268. cp REFLECT_EFFECT + 1
  5269. jr c, .playerMistCheck
  5270. jr .skipPlayerMistCheck
  5271. .playerMistCheck
  5272. ; similar to enemy mist check
  5273. ld a, [wPlayerBattleStatus2]
  5274. bit PROTECTED_BY_MIST, a ; is mon protected by mist?
  5275. jp nz, .moveMissed
  5276. .skipPlayerMistCheck
  5277. ld a, [wEnemyBattleStatus2]
  5278. bit USING_X_ACCURACY, a ; is the enemy using X Accuracy?
  5279. ret nz ; if so, always hit regardless of accuracy/evasion
  5280. .calcHitChance
  5281. call CalcHitChance ; scale the move accuracy according to attacker's accuracy and target's evasion
  5282. ld a, [wPlayerMoveAccuracy]
  5283. ld b, a
  5284. ld a, [H_WHOSETURN]
  5285. and a
  5286. jr z, .doAccuracyCheck
  5287. ld a, [wEnemyMoveAccuracy]
  5288. ld b, a
  5289. .doAccuracyCheck
  5290. ; if the random number generated is greater than or equal to the scaled accuracy, the move misses
  5291. ; note that this means that even the highest accuracy is still just a 255/256 chance, not 100%
  5292. call BattleRandom
  5293. cp b
  5294. jr nc, .moveMissed
  5295. ret
  5296. .moveMissed
  5297. xor a
  5298. ld hl, wDamage ; zero the damage
  5299. ld [hli], a
  5300. ld [hl], a
  5301. inc a
  5302. ld [wMoveMissed], a
  5303. ld a, [H_WHOSETURN]
  5304. and a
  5305. jr z, .playerTurn2
  5306. .enemyTurn2
  5307. ld hl, wEnemyBattleStatus1
  5308. res USING_TRAPPING_MOVE, [hl] ; end multi-turn attack e.g. wrap
  5309. ret
  5310. .playerTurn2
  5311. ld hl, wPlayerBattleStatus1
  5312. res USING_TRAPPING_MOVE, [hl] ; end multi-turn attack e.g. wrap
  5313. ret
  5314. ; values for player turn
  5315. CalcHitChance:
  5316. ld hl, wPlayerMoveAccuracy
  5317. ld a, [H_WHOSETURN]
  5318. and a
  5319. ld a, [wPlayerMonAccuracyMod]
  5320. ld b, a
  5321. ld a, [wEnemyMonEvasionMod]
  5322. ld c, a
  5323. jr z, .next
  5324. ; values for enemy turn
  5325. ld hl, wEnemyMoveAccuracy
  5326. ld a, [wEnemyMonAccuracyMod]
  5327. ld b, a
  5328. ld a, [wPlayerMonEvasionMod]
  5329. ld c, a
  5330. .next
  5331. ld a, $0e
  5332. sub c
  5333. ld c, a ; c = 14 - EVASIONMOD (this "reflects" the value over 7, so that an increase in the target's evasion
  5334. ; decreases the hit chance instead of increasing the hit chance)
  5335. ; zero the high bytes of the multiplicand
  5336. xor a
  5337. ld [H_MULTIPLICAND], a
  5338. ld [H_MULTIPLICAND + 1], a
  5339. ld a, [hl]
  5340. ld [H_MULTIPLICAND + 2], a ; set multiplicand to move accuracy
  5341. push hl
  5342. ld d, $02 ; loop has two iterations
  5343. ; loop to do the calculations, the first iteration multiplies by the accuracy ratio and
  5344. ; the second iteration multiplies by the evasion ratio
  5345. .loop
  5346. push bc
  5347. ld hl, StatModifierRatios ; stat modifier ratios
  5348. dec b
  5349. sla b
  5350. ld c, b
  5351. ld b, $00
  5352. add hl, bc ; hl = address of stat modifier ratio
  5353. pop bc
  5354. ld a, [hli]
  5355. ld [H_MULTIPLIER], a ; set multiplier to the numerator of the ratio
  5356. call Multiply
  5357. ld a, [hl]
  5358. ld [H_DIVISOR], a ; set divisor to the the denominator of the ratio
  5359. ; (the dividend is the product of the previous multiplication)
  5360. ld b, $04 ; number of bytes in the dividend
  5361. call Divide
  5362. ld a, [H_QUOTIENT + 3]
  5363. ld b, a
  5364. ld a, [H_QUOTIENT + 2]
  5365. or b
  5366. jp nz, .nextCalculation
  5367. ; make sure the result is always at least one
  5368. ld [H_QUOTIENT + 2], a
  5369. ld a, $01
  5370. ld [H_QUOTIENT + 3], a
  5371. .nextCalculation
  5372. ld b, c
  5373. dec d
  5374. jr nz, .loop
  5375. ld a, [H_QUOTIENT + 2]
  5376. and a ; is the calculated hit chance over 0xFF?
  5377. ld a, [H_QUOTIENT + 3]
  5378. jr z, .storeAccuracy
  5379. ; if calculated hit chance over 0xFF
  5380. ld a, $ff ; set the hit chance to 0xFF
  5381. .storeAccuracy
  5382. pop hl
  5383. ld [hl], a ; store the hit chance in the move accuracy variable
  5384. ret
  5385. ; multiplies damage by a random percentage from ~85% to 100%
  5386. RandomizeDamage:
  5387. ld hl, wDamage
  5388. ld a, [hli]
  5389. and a
  5390. jr nz, .DamageGreaterThanOne
  5391. ld a, [hl]
  5392. cp 2
  5393. ret c ; return if damage is equal to 0 or 1
  5394. .DamageGreaterThanOne
  5395. xor a
  5396. ld [H_MULTIPLICAND], a
  5397. dec hl
  5398. ld a, [hli]
  5399. ld [H_MULTIPLICAND + 1], a
  5400. ld a, [hl]
  5401. ld [H_MULTIPLICAND + 2], a
  5402. ; loop until a random number greater than or equal to 217 is generated
  5403. .loop
  5404. call BattleRandom
  5405. rrca
  5406. cp 217
  5407. jr c, .loop
  5408. ld [H_MULTIPLIER], a
  5409. call Multiply ; multiply damage by the random number, which is in the range [217, 255]
  5410. ld a, 255
  5411. ld [H_DIVISOR], a
  5412. ld b, $4
  5413. call Divide ; divide the result by 255
  5414. ; store the modified damage
  5415. ld a, [H_QUOTIENT + 2]
  5416. ld hl, wDamage
  5417. ld [hli], a
  5418. ld a, [H_QUOTIENT + 3]
  5419. ld [hl], a
  5420. ret
  5421. ; for more detailed commentary, see equivalent function for player side (ExecutePlayerMove)
  5422. ExecuteEnemyMove:
  5423. ld a, [wEnemySelectedMove]
  5424. inc a
  5425. jp z, ExecuteEnemyMoveDone
  5426. call PrintGhostText
  5427. jp z, ExecuteEnemyMoveDone
  5428. ld a, [wLinkState]
  5429. cp LINK_STATE_BATTLING
  5430. jr nz, .executeEnemyMove
  5431. ld b, $1
  5432. ld a, [wSerialExchangeNybbleReceiveData]
  5433. cp LINKBATTLE_STRUGGLE
  5434. jr z, .executeEnemyMove
  5435. cp 4
  5436. ret nc
  5437. .executeEnemyMove
  5438. ld hl, wAILayer2Encouragement
  5439. inc [hl]
  5440. xor a
  5441. ld [wMoveMissed], a
  5442. ld [wMoveDidntMiss], a
  5443. ld a, $a
  5444. ld [wDamageMultipliers], a
  5445. call CheckEnemyStatusConditions
  5446. jr nz, .enemyHasNoSpecialConditions
  5447. jp hl
  5448. .enemyHasNoSpecialConditions
  5449. ld hl, wEnemyBattleStatus1
  5450. bit CHARGING_UP, [hl] ; is the enemy charging up for attack?
  5451. jr nz, EnemyCanExecuteChargingMove ; if so, jump
  5452. call GetCurrentMove
  5453. CheckIfEnemyNeedsToChargeUp:
  5454. ld a, [wEnemyMoveEffect]
  5455. cp CHARGE_EFFECT
  5456. jp z, JumpMoveEffect
  5457. cp FLY_EFFECT
  5458. jp z, JumpMoveEffect
  5459. jr EnemyCanExecuteMove
  5460. EnemyCanExecuteChargingMove:
  5461. ld hl, wEnemyBattleStatus1
  5462. res CHARGING_UP, [hl] ; no longer charging up for attack
  5463. res INVULNERABLE, [hl] ; no longer invulnerable to typical attacks
  5464. ld a, [wEnemyMoveNum]
  5465. ld [wd0b5], a
  5466. ld a, BANK(MoveNames)
  5467. ld [wPredefBank], a
  5468. ld a, MOVE_NAME
  5469. ld [wNameListType], a
  5470. call GetName
  5471. ld de, wcd6d
  5472. call CopyStringToCF4B
  5473. EnemyCanExecuteMove:
  5474. xor a
  5475. ld [wMonIsDisobedient], a
  5476. call PrintMonName1Text
  5477. ld a, [wEnemyMoveEffect]
  5478. ld hl, ResidualEffects1
  5479. ld de, $1
  5480. call IsInArray
  5481. jp c, JumpMoveEffect
  5482. ld a, [wEnemyMoveEffect]
  5483. ld hl, SpecialEffectsCont
  5484. ld de, $1
  5485. call IsInArray
  5486. call c, JumpMoveEffect
  5487. EnemyCalcMoveDamage:
  5488. call SwapPlayerAndEnemyLevels
  5489. ld a, [wEnemyMoveEffect]
  5490. ld hl, SetDamageEffects
  5491. ld de, $1
  5492. call IsInArray
  5493. jp c, EnemyMoveHitTest
  5494. call CriticalHitTest
  5495. call HandleCounterMove
  5496. jr z, handleIfEnemyMoveMissed
  5497. call SwapPlayerAndEnemyLevels
  5498. call GetDamageVarsForEnemyAttack
  5499. call SwapPlayerAndEnemyLevels
  5500. call CalculateDamage
  5501. jp z, EnemyCheckIfFlyOrChargeEffect
  5502. call AdjustDamageForMoveType
  5503. call RandomizeDamage
  5504. EnemyMoveHitTest:
  5505. call MoveHitTest
  5506. handleIfEnemyMoveMissed:
  5507. ld a, [wMoveMissed]
  5508. and a
  5509. jr z, .moveDidNotMiss
  5510. ld a, [wEnemyMoveEffect]
  5511. cp EXPLODE_EFFECT
  5512. jr z, handleExplosionMiss
  5513. jr EnemyCheckIfFlyOrChargeEffect
  5514. .moveDidNotMiss
  5515. call SwapPlayerAndEnemyLevels
  5516. GetEnemyAnimationType:
  5517. ld a, [wEnemyMoveEffect]
  5518. and a
  5519. ld a, $1
  5520. jr z, playEnemyMoveAnimation
  5521. ld a, $2
  5522. jr playEnemyMoveAnimation
  5523. handleExplosionMiss:
  5524. call SwapPlayerAndEnemyLevels
  5525. xor a
  5526. playEnemyMoveAnimation:
  5527. push af
  5528. ld a, [wEnemyBattleStatus2]
  5529. bit HAS_SUBSTITUTE_UP, a ; does mon have a substitute?
  5530. ld hl, HideSubstituteShowMonAnim
  5531. ld b, BANK(HideSubstituteShowMonAnim)
  5532. call nz, Bankswitch
  5533. pop af
  5534. ld [wAnimationType], a
  5535. ld a, [wEnemyMoveNum]
  5536. call PlayMoveAnimation
  5537. call HandleExplodingAnimation
  5538. call DrawEnemyHUDAndHPBar
  5539. ld a, [wEnemyBattleStatus2]
  5540. bit HAS_SUBSTITUTE_UP, a ; does mon have a substitute?
  5541. ld hl, ReshowSubstituteAnim
  5542. ld b, BANK(ReshowSubstituteAnim)
  5543. call nz, Bankswitch ; slide the substitute's sprite out
  5544. jr EnemyCheckIfMirrorMoveEffect
  5545. EnemyCheckIfFlyOrChargeEffect:
  5546. call SwapPlayerAndEnemyLevels
  5547. ld c, 30
  5548. call DelayFrames
  5549. ld a, [wEnemyMoveEffect]
  5550. cp FLY_EFFECT
  5551. jr z, .playAnim
  5552. cp CHARGE_EFFECT
  5553. jr z, .playAnim
  5554. jr EnemyCheckIfMirrorMoveEffect
  5555. .playAnim
  5556. xor a
  5557. ld [wAnimationType], a
  5558. ld a, STATUS_AFFECTED_ANIM
  5559. call PlayMoveAnimation
  5560. EnemyCheckIfMirrorMoveEffect:
  5561. ld a, [wEnemyMoveEffect]
  5562. cp MIRROR_MOVE_EFFECT
  5563. jr nz, .notMirrorMoveEffect
  5564. call MirrorMoveCopyMove
  5565. jp z, ExecuteEnemyMoveDone
  5566. jp CheckIfEnemyNeedsToChargeUp
  5567. .notMirrorMoveEffect
  5568. cp METRONOME_EFFECT
  5569. jr nz, .notMetronomeEffect
  5570. call MetronomePickMove
  5571. jp CheckIfEnemyNeedsToChargeUp
  5572. .notMetronomeEffect
  5573. ld a, [wEnemyMoveEffect]
  5574. ld hl, ResidualEffects2
  5575. ld de, $1
  5576. call IsInArray
  5577. jp c, JumpMoveEffect
  5578. ld a, [wMoveMissed]
  5579. and a
  5580. jr z, .moveDidNotMiss
  5581. call PrintMoveFailureText
  5582. ld a, [wEnemyMoveEffect]
  5583. cp EXPLODE_EFFECT
  5584. jr z, .handleExplosionMiss
  5585. jp ExecuteEnemyMoveDone
  5586. .moveDidNotMiss
  5587. call ApplyAttackToPlayerPokemon
  5588. call PrintCriticalOHKOText
  5589. callab DisplayEffectiveness
  5590. ld a, 1
  5591. ld [wMoveDidntMiss], a
  5592. .handleExplosionMiss
  5593. ld a, [wEnemyMoveEffect]
  5594. ld hl, AlwaysHappenSideEffects
  5595. ld de, $1
  5596. call IsInArray
  5597. call c, JumpMoveEffect
  5598. ld hl, wBattleMonHP
  5599. ld a, [hli]
  5600. ld b, [hl]
  5601. or b
  5602. ret z
  5603. call HandleBuildingRage
  5604. ld hl, wEnemyBattleStatus1
  5605. bit ATTACKING_MULTIPLE_TIMES, [hl] ; is mon hitting multiple times? (example: double kick)
  5606. jr z, .notMultiHitMove
  5607. push hl
  5608. ld hl, wEnemyNumAttacksLeft
  5609. dec [hl]
  5610. pop hl
  5611. jp nz, GetEnemyAnimationType
  5612. res ATTACKING_MULTIPLE_TIMES, [hl] ; mon is no longer hitting multiple times
  5613. ld hl, HitXTimesText
  5614. call PrintText
  5615. xor a
  5616. ld [wEnemyNumHits], a
  5617. .notMultiHitMove
  5618. ld a, [wEnemyMoveEffect]
  5619. and a
  5620. jr z, ExecuteEnemyMoveDone
  5621. ld hl, SpecialEffects
  5622. ld de, $1
  5623. call IsInArray
  5624. call nc, JumpMoveEffect
  5625. jr ExecuteEnemyMoveDone
  5626. HitXTimesText:
  5627. TX_FAR _HitXTimesText
  5628. db "@"
  5629. ExecuteEnemyMoveDone:
  5630. ld b, $1
  5631. ret
  5632. ; checks for various status conditions affecting the enemy mon
  5633. ; stores whether the mon cannot use a move this turn in Z flag
  5634. CheckEnemyStatusConditions:
  5635. ld hl, wEnemyMonStatus
  5636. ld a, [hl]
  5637. and SLP ; sleep mask
  5638. jr z, .checkIfFrozen
  5639. dec a ; decrement number of turns left
  5640. ld [wEnemyMonStatus], a
  5641. and a
  5642. jr z, .wokeUp ; if the number of turns hit 0, wake up
  5643. ld hl, FastAsleepText
  5644. call PrintText
  5645. xor a
  5646. ld [wAnimationType], a
  5647. ld a, SLP_ANIM
  5648. call PlayMoveAnimation
  5649. jr .sleepDone
  5650. .wokeUp
  5651. ld hl, WokeUpText
  5652. call PrintText
  5653. .sleepDone
  5654. xor a
  5655. ld [wEnemyUsedMove], a
  5656. ld hl, ExecuteEnemyMoveDone ; enemy can't move this turn
  5657. jp .enemyReturnToHL
  5658. .checkIfFrozen
  5659. bit FRZ, [hl]
  5660. jr z, .checkIfTrapped
  5661. ld hl, IsFrozenText
  5662. call PrintText
  5663. xor a
  5664. ld [wEnemyUsedMove], a
  5665. ld hl, ExecuteEnemyMoveDone ; enemy can't move this turn
  5666. jp .enemyReturnToHL
  5667. .checkIfTrapped
  5668. ld a, [wPlayerBattleStatus1]
  5669. bit USING_TRAPPING_MOVE, a ; is the player using a multi-turn attack like warp
  5670. jp z, .checkIfFlinched
  5671. ld hl, CantMoveText
  5672. call PrintText
  5673. ld hl, ExecuteEnemyMoveDone ; enemy can't move this turn
  5674. jp .enemyReturnToHL
  5675. .checkIfFlinched
  5676. ld hl, wEnemyBattleStatus1
  5677. bit FLINCHED, [hl] ; check if enemy mon flinched
  5678. jp z, .checkIfMustRecharge
  5679. res FLINCHED, [hl]
  5680. ld hl, FlinchedText
  5681. call PrintText
  5682. ld hl, ExecuteEnemyMoveDone ; enemy can't move this turn
  5683. jp .enemyReturnToHL
  5684. .checkIfMustRecharge
  5685. ld hl, wEnemyBattleStatus2
  5686. bit NEEDS_TO_RECHARGE, [hl] ; check if enemy mon has to recharge after using a move
  5687. jr z, .checkIfAnyMoveDisabled
  5688. res NEEDS_TO_RECHARGE, [hl]
  5689. ld hl, MustRechargeText
  5690. call PrintText
  5691. ld hl, ExecuteEnemyMoveDone ; enemy can't move this turn
  5692. jp .enemyReturnToHL
  5693. .checkIfAnyMoveDisabled
  5694. ld hl, wEnemyDisabledMove
  5695. ld a, [hl]
  5696. and a
  5697. jr z, .checkIfConfused
  5698. dec a ; decrement disable counter
  5699. ld [hl], a
  5700. and $f ; did disable counter hit 0?
  5701. jr nz, .checkIfConfused
  5702. ld [hl], a
  5703. ld [wEnemyDisabledMoveNumber], a
  5704. ld hl, DisabledNoMoreText
  5705. call PrintText
  5706. .checkIfConfused
  5707. ld a, [wEnemyBattleStatus1]
  5708. add a ; check if enemy mon is confused
  5709. jp nc, .checkIfTriedToUseDisabledMove
  5710. ld hl, wEnemyConfusedCounter
  5711. dec [hl]
  5712. jr nz, .isConfused
  5713. ld hl, wEnemyBattleStatus1
  5714. res CONFUSED, [hl] ; if confused counter hit 0, reset confusion status
  5715. ld hl, ConfusedNoMoreText
  5716. call PrintText
  5717. jp .checkIfTriedToUseDisabledMove
  5718. .isConfused
  5719. ld hl, IsConfusedText
  5720. call PrintText
  5721. xor a
  5722. ld [wAnimationType], a
  5723. ld a, CONF_ANIM
  5724. call PlayMoveAnimation
  5725. call BattleRandom
  5726. cp $80
  5727. jr c, .checkIfTriedToUseDisabledMove
  5728. ld hl, wEnemyBattleStatus1
  5729. ld a, [hl]
  5730. and 1 << CONFUSED ; if mon hurts itself, clear every other status from wEnemyBattleStatus1
  5731. ld [hl], a
  5732. ld hl, HurtItselfText
  5733. call PrintText
  5734. ld hl, wBattleMonDefense
  5735. ld a, [hli]
  5736. push af
  5737. ld a, [hld]
  5738. push af
  5739. ld a, [wEnemyMonDefense]
  5740. ld [hli], a
  5741. ld a, [wEnemyMonDefense + 1]
  5742. ld [hl], a
  5743. ld hl, wEnemyMoveEffect
  5744. push hl
  5745. ld a, [hl]
  5746. push af
  5747. xor a
  5748. ld [hli], a
  5749. ld [wCriticalHitOrOHKO], a
  5750. ld a, 40
  5751. ld [hli], a
  5752. xor a
  5753. ld [hl], a
  5754. call GetDamageVarsForEnemyAttack
  5755. call CalculateDamage
  5756. pop af
  5757. pop hl
  5758. ld [hl], a
  5759. ld hl, wBattleMonDefense + 1
  5760. pop af
  5761. ld [hld], a
  5762. pop af
  5763. ld [hl], a
  5764. xor a
  5765. ld [wAnimationType], a
  5766. ld [H_WHOSETURN], a
  5767. ld a, POUND
  5768. call PlayMoveAnimation
  5769. ld a, $1
  5770. ld [H_WHOSETURN], a
  5771. call ApplyDamageToEnemyPokemon
  5772. jr .monHurtItselfOrFullyParalysed
  5773. .checkIfTriedToUseDisabledMove
  5774. ; prevents a disabled move that was selected before being disabled from being used
  5775. ld a, [wEnemyDisabledMoveNumber]
  5776. and a
  5777. jr z, .checkIfParalysed
  5778. ld hl, wEnemySelectedMove
  5779. cp [hl]
  5780. jr nz, .checkIfParalysed
  5781. call PrintMoveIsDisabledText
  5782. ld hl, ExecuteEnemyMoveDone ; if a disabled move was somehow selected, player can't move this turn
  5783. jp .enemyReturnToHL
  5784. .checkIfParalysed
  5785. ld hl, wEnemyMonStatus
  5786. bit PAR, [hl]
  5787. jr z, .checkIfUsingBide
  5788. call BattleRandom
  5789. cp $3f ; 25% to be fully paralysed
  5790. jr nc, .checkIfUsingBide
  5791. ld hl, FullyParalyzedText
  5792. call PrintText
  5793. .monHurtItselfOrFullyParalysed
  5794. ld hl, wEnemyBattleStatus1
  5795. ld a, [hl]
  5796. ; clear bide, thrashing about, charging up, and multi-turn moves such as warp
  5797. and $ff ^ ((1 << STORING_ENERGY) | (1 << THRASHING_ABOUT) | (1 << CHARGING_UP) | (1 << USING_TRAPPING_MOVE))
  5798. ld [hl], a
  5799. ld a, [wEnemyMoveEffect]
  5800. cp FLY_EFFECT
  5801. jr z, .flyOrChargeEffect
  5802. cp CHARGE_EFFECT
  5803. jr z, .flyOrChargeEffect
  5804. jr .notFlyOrChargeEffect
  5805. .flyOrChargeEffect
  5806. xor a
  5807. ld [wAnimationType], a
  5808. ld a, STATUS_AFFECTED_ANIM
  5809. call PlayMoveAnimation
  5810. .notFlyOrChargeEffect
  5811. ld hl, ExecuteEnemyMoveDone
  5812. jp .enemyReturnToHL ; if using a two-turn move, enemy needs to recharge the first turn
  5813. .checkIfUsingBide
  5814. ld hl, wEnemyBattleStatus1
  5815. bit STORING_ENERGY, [hl] ; is mon using bide?
  5816. jr z, .checkIfThrashingAbout
  5817. xor a
  5818. ld [wEnemyMoveNum], a
  5819. ld hl, wDamage
  5820. ld a, [hli]
  5821. ld b, a
  5822. ld c, [hl]
  5823. ld hl, wEnemyBideAccumulatedDamage + 1
  5824. ld a, [hl]
  5825. add c ; accumulate damage taken
  5826. ld [hld], a
  5827. ld a, [hl]
  5828. adc b
  5829. ld [hl], a
  5830. ld hl, wEnemyNumAttacksLeft
  5831. dec [hl] ; did Bide counter hit 0?
  5832. jr z, .unleashEnergy
  5833. ld hl, ExecuteEnemyMoveDone
  5834. jp .enemyReturnToHL ; unless mon unleashes energy, can't move this turn
  5835. .unleashEnergy
  5836. ld hl, wEnemyBattleStatus1
  5837. res STORING_ENERGY, [hl] ; not using bide any more
  5838. ld hl, UnleashedEnergyText
  5839. call PrintText
  5840. ld a, $1
  5841. ld [wEnemyMovePower], a
  5842. ld hl, wEnemyBideAccumulatedDamage + 1
  5843. ld a, [hld]
  5844. add a
  5845. ld b, a
  5846. ld [wDamage + 1], a
  5847. ld a, [hl]
  5848. rl a ; double the damage
  5849. ld [wDamage], a
  5850. or b
  5851. jr nz, .next
  5852. ld a, $1
  5853. ld [wMoveMissed], a
  5854. .next
  5855. xor a
  5856. ld [hli], a
  5857. ld [hl], a
  5858. ld a, BIDE
  5859. ld [wEnemyMoveNum], a
  5860. call SwapPlayerAndEnemyLevels
  5861. ld hl, handleIfEnemyMoveMissed ; skip damage calculation, DecrementPP and MoveHitTest
  5862. jp .enemyReturnToHL
  5863. .checkIfThrashingAbout
  5864. bit THRASHING_ABOUT, [hl] ; is mon using thrash or petal dance?
  5865. jr z, .checkIfUsingMultiturnMove
  5866. ld a, THRASH
  5867. ld [wEnemyMoveNum], a
  5868. ld hl, ThrashingAboutText
  5869. call PrintText
  5870. ld hl, wEnemyNumAttacksLeft
  5871. dec [hl] ; did Thrashing About counter hit 0?
  5872. ld hl, EnemyCalcMoveDamage ; skip DecrementPP
  5873. jp nz, .enemyReturnToHL
  5874. push hl
  5875. ld hl, wEnemyBattleStatus1
  5876. res THRASHING_ABOUT, [hl] ; mon is no longer using thrash or petal dance
  5877. set CONFUSED, [hl] ; mon is now confused
  5878. call BattleRandom
  5879. and $3
  5880. inc a
  5881. inc a ; confused for 2-5 turns
  5882. ld [wEnemyConfusedCounter], a
  5883. pop hl ; skip DecrementPP
  5884. jp .enemyReturnToHL
  5885. .checkIfUsingMultiturnMove
  5886. bit USING_TRAPPING_MOVE, [hl] ; is mon using multi-turn move?
  5887. jp z, .checkIfUsingRage
  5888. ld hl, AttackContinuesText
  5889. call PrintText
  5890. ld hl, wEnemyNumAttacksLeft
  5891. dec [hl] ; did multi-turn move end?
  5892. ld hl, GetEnemyAnimationType ; if it didn't, skip damage calculation (deal damage equal to last hit),
  5893. ; DecrementPP and MoveHitTest
  5894. jp nz, .enemyReturnToHL
  5895. jp .enemyReturnToHL
  5896. .checkIfUsingRage
  5897. ld a, [wEnemyBattleStatus2]
  5898. bit USING_RAGE, a ; is mon using rage?
  5899. jp z, .checkEnemyStatusConditionsDone ; if we made it this far, mon can move normally this turn
  5900. ld a, RAGE
  5901. ld [wd11e], a
  5902. call GetMoveName
  5903. call CopyStringToCF4B
  5904. xor a
  5905. ld [wEnemyMoveEffect], a
  5906. ld hl, EnemyCanExecuteMove
  5907. jp .enemyReturnToHL
  5908. .enemyReturnToHL
  5909. xor a ; set Z flag
  5910. ret
  5911. .checkEnemyStatusConditionsDone
  5912. ld a, $1
  5913. and a ; clear Z flag
  5914. ret
  5915. GetCurrentMove:
  5916. ld a, [H_WHOSETURN]
  5917. and a
  5918. jp z, .player
  5919. ld de, wEnemyMoveNum
  5920. ld a, [wEnemySelectedMove]
  5921. jr .selected
  5922. .player
  5923. ld de, wPlayerMoveNum
  5924. ld a, [wFlags_D733]
  5925. bit BIT_TEST_BATTLE, a
  5926. ld a, [wTestBattlePlayerSelectedMove]
  5927. jr nz, .selected
  5928. ld a, [wPlayerSelectedMove]
  5929. .selected
  5930. ld [wd0b5], a
  5931. dec a
  5932. ld hl, Moves
  5933. ld bc, MoveEnd - Moves
  5934. call AddNTimes
  5935. ld a, BANK(Moves)
  5936. call FarCopyData
  5937. ld a, BANK(MoveNames)
  5938. ld [wPredefBank], a
  5939. ld a, MOVE_NAME
  5940. ld [wNameListType], a
  5941. call GetName
  5942. ld de, wcd6d
  5943. jp CopyStringToCF4B
  5944. LoadEnemyMonData:
  5945. ld a, [wLinkState]
  5946. cp LINK_STATE_BATTLING
  5947. jp z, LoadEnemyMonFromParty
  5948. ld a, [wEnemyMonSpecies2]
  5949. ld [wEnemyMonSpecies], a
  5950. ld [wd0b5], a
  5951. call GetMonHeader
  5952. ld a, [wEnemyBattleStatus3]
  5953. bit TRANSFORMED, a ; is enemy mon transformed?
  5954. ld hl, wTransformedEnemyMonOriginalDVs ; original DVs before transforming
  5955. ld a, [hli]
  5956. ld b, [hl]
  5957. jr nz, .storeDVs
  5958. ld a, [wIsInBattle]
  5959. cp $2 ; is it a trainer battle?
  5960. ; fixed DVs for trainer mon
  5961. ld a, $98
  5962. ld b, $88
  5963. jr z, .storeDVs
  5964. ; random DVs for wild mon
  5965. call BattleRandom
  5966. ld b, a
  5967. call BattleRandom
  5968. .storeDVs
  5969. ld hl, wEnemyMonDVs
  5970. ld [hli], a
  5971. ld [hl], b
  5972. ld de, wEnemyMonLevel
  5973. ld a, [wCurEnemyLVL]
  5974. ld [de], a
  5975. inc de
  5976. ld b, $0
  5977. ld hl, wEnemyMonHP
  5978. push hl
  5979. call CalcStats
  5980. pop hl
  5981. ld a, [wIsInBattle]
  5982. cp $2 ; is it a trainer battle?
  5983. jr z, .copyHPAndStatusFromPartyData
  5984. ld a, [wEnemyBattleStatus3]
  5985. bit TRANSFORMED, a ; is enemy mon transformed?
  5986. jr nz, .copyTypes ; if transformed, jump
  5987. ; if it's a wild mon and not transformed, init the current HP to max HP and the status to 0
  5988. ld a, [wEnemyMonMaxHP]
  5989. ld [hli], a
  5990. ld a, [wEnemyMonMaxHP+1]
  5991. ld [hli], a
  5992. xor a
  5993. inc hl
  5994. ld [hl], a ; init status to 0
  5995. jr .copyTypes
  5996. ; if it's a trainer mon, copy the HP and status from the enemy party data
  5997. .copyHPAndStatusFromPartyData
  5998. ld hl, wEnemyMon1HP
  5999. ld a, [wWhichPokemon]
  6000. ld bc, wEnemyMon2 - wEnemyMon1
  6001. call AddNTimes
  6002. ld a, [hli]
  6003. ld [wEnemyMonHP], a
  6004. ld a, [hli]
  6005. ld [wEnemyMonHP + 1], a
  6006. ld a, [wWhichPokemon]
  6007. ld [wEnemyMonPartyPos], a
  6008. inc hl
  6009. ld a, [hl]
  6010. ld [wEnemyMonStatus], a
  6011. jr .copyTypes
  6012. .copyTypes
  6013. ld hl, wMonHTypes
  6014. ld de, wEnemyMonType
  6015. ld a, [hli] ; copy type 1
  6016. ld [de], a
  6017. inc de
  6018. ld a, [hli] ; copy type 2
  6019. ld [de], a
  6020. inc de
  6021. ld a, [hli] ; copy catch rate
  6022. ld [de], a
  6023. inc de
  6024. ld a, [wIsInBattle]
  6025. cp $2 ; is it a trainer battle?
  6026. jr nz, .copyStandardMoves
  6027. ; if it's a trainer battle, copy moves from enemy party data
  6028. ld hl, wEnemyMon1Moves
  6029. ld a, [wWhichPokemon]
  6030. ld bc, wEnemyMon2 - wEnemyMon1
  6031. call AddNTimes
  6032. ld bc, NUM_MOVES
  6033. call CopyData
  6034. jr .loadMovePPs
  6035. .copyStandardMoves
  6036. ; for a wild mon, first copy default moves from the mon header
  6037. ld hl, wMonHMoves
  6038. ld a, [hli]
  6039. ld [de], a
  6040. inc de
  6041. ld a, [hli]
  6042. ld [de], a
  6043. inc de
  6044. ld a, [hli]
  6045. ld [de], a
  6046. inc de
  6047. ld a, [hl]
  6048. ld [de], a
  6049. dec de
  6050. dec de
  6051. dec de
  6052. xor a
  6053. ld [wLearningMovesFromDayCare], a
  6054. predef WriteMonMoves ; get moves based on current level
  6055. .loadMovePPs
  6056. ld hl, wEnemyMonMoves
  6057. ld de, wEnemyMonPP - 1
  6058. predef LoadMovePPs
  6059. ld hl, wMonHBaseStats
  6060. ld de, wEnemyMonBaseStats
  6061. ld b, NUM_STATS
  6062. .copyBaseStatsLoop
  6063. ld a, [hli]
  6064. ld [de], a
  6065. inc de
  6066. dec b
  6067. jr nz, .copyBaseStatsLoop
  6068. ld hl, wMonHCatchRate
  6069. ld a, [hli]
  6070. ld [de], a
  6071. inc de
  6072. ld a, [hl] ; base exp
  6073. ld [de], a
  6074. ld a, [wEnemyMonSpecies2]
  6075. ld [wd11e], a
  6076. call GetMonName
  6077. ld hl, wcd6d
  6078. ld de, wEnemyMonNick
  6079. ld bc, NAME_LENGTH
  6080. call CopyData
  6081. ld a, [wEnemyMonSpecies2]
  6082. ld [wd11e], a
  6083. predef IndexToPokedex
  6084. ld a, [wd11e]
  6085. dec a
  6086. ld c, a
  6087. ld b, FLAG_SET
  6088. ld hl, wPokedexSeen
  6089. predef FlagActionPredef ; mark this mon as seen in the pokedex
  6090. ld hl, wEnemyMonLevel
  6091. ld de, wEnemyMonUnmodifiedLevel
  6092. ld bc, 1 + NUM_STATS * 2
  6093. call CopyData
  6094. ld a, $7 ; default stat mod
  6095. ld b, NUM_STAT_MODS ; number of stat mods
  6096. ld hl, wEnemyMonStatMods
  6097. .statModLoop
  6098. ld [hli], a
  6099. dec b
  6100. jr nz, .statModLoop
  6101. ret
  6102. ; calls BattleTransition to show the battle transition animation and initializes some battle variables
  6103. DoBattleTransitionAndInitBattleVariables:
  6104. ld a, [wLinkState]
  6105. cp LINK_STATE_BATTLING
  6106. jr nz, .next
  6107. ; link battle
  6108. xor a
  6109. ld [wMenuJoypadPollCount], a
  6110. callab DisplayLinkBattleVersusTextBox
  6111. ld a, $1
  6112. ld [wUpdateSpritesEnabled], a
  6113. call ClearScreen
  6114. .next
  6115. call DelayFrame
  6116. predef BattleTransition
  6117. callab LoadHudAndHpBarAndStatusTilePatterns
  6118. ld a, $1
  6119. ld [H_AUTOBGTRANSFERENABLED], a
  6120. ld a, $ff
  6121. ld [wUpdateSpritesEnabled], a
  6122. call ClearSprites
  6123. call ClearScreen
  6124. xor a
  6125. ld [H_AUTOBGTRANSFERENABLED], a
  6126. ld [hWY], a
  6127. ld [rWY], a
  6128. ld [hTilesetType], a
  6129. ld hl, wPlayerStatsToDouble
  6130. ld [hli], a
  6131. ld [hli], a
  6132. ld [hli], a
  6133. ld [hli], a
  6134. ld [hl], a
  6135. ld [wPlayerDisabledMove], a
  6136. ret
  6137. ; swaps the level values of the BattleMon and EnemyMon structs
  6138. SwapPlayerAndEnemyLevels:
  6139. push bc
  6140. ld a, [wBattleMonLevel]
  6141. ld b, a
  6142. ld a, [wEnemyMonLevel]
  6143. ld [wBattleMonLevel], a
  6144. ld a, b
  6145. ld [wEnemyMonLevel], a
  6146. pop bc
  6147. ret
  6148. ; loads either red back pic or old man back pic
  6149. ; also writes OAM data and loads tile patterns for the Red or Old Man back sprite's head
  6150. ; (for use when scrolling the player sprite and enemy's silhouettes on screen)
  6151. LoadPlayerBackPic:
  6152. ld a, [wBattleType]
  6153. dec a ; is it the old man tutorial?
  6154. ld de, RedPicBack
  6155. jr nz, .next
  6156. ld de, OldManPic
  6157. .next
  6158. ld a, BANK(RedPicBack)
  6159. call UncompressSpriteFromDE
  6160. predef ScaleSpriteByTwo
  6161. ld hl, wOAMBuffer
  6162. xor a
  6163. ld [hOAMTile], a ; initial tile number
  6164. ld b, $7 ; 7 columns
  6165. ld e, $a0 ; X for the left-most column
  6166. .loop ; each loop iteration writes 3 OAM entries in a vertical column
  6167. ld c, $3 ; 3 tiles per column
  6168. ld d, $38 ; Y for the top of each column
  6169. .innerLoop ; each loop iteration writes 1 OAM entry in the column
  6170. ld [hl], d ; OAM Y
  6171. inc hl
  6172. ld [hl], e ; OAM X
  6173. ld a, $8 ; height of tile
  6174. add d ; increase Y by height of tile
  6175. ld d, a
  6176. inc hl
  6177. ld a, [hOAMTile]
  6178. ld [hli], a ; OAM tile number
  6179. inc a ; increment tile number
  6180. ld [hOAMTile], a
  6181. inc hl
  6182. dec c
  6183. jr nz, .innerLoop
  6184. ld a, [hOAMTile]
  6185. add $4 ; increase tile number by 4
  6186. ld [hOAMTile], a
  6187. ld a, $8 ; width of tile
  6188. add e ; increase X by width of tile
  6189. ld e, a
  6190. dec b
  6191. jr nz, .loop
  6192. ld de, vBackPic
  6193. call InterlaceMergeSpriteBuffers
  6194. ld a, $a
  6195. ld [$0], a
  6196. xor a
  6197. ld [$4000], a
  6198. ld hl, vSprites
  6199. ld de, sSpriteBuffer1
  6200. ld a, [H_LOADEDROMBANK]
  6201. ld b, a
  6202. ld c, 7 * 7
  6203. call CopyVideoData
  6204. xor a
  6205. ld [$0], a
  6206. ld a, $31
  6207. ld [hStartTileID], a
  6208. coord hl, 1, 5
  6209. predef_jump CopyUncompressedPicToTilemap
  6210. ; does nothing since no stats are ever selected (barring glitches)
  6211. DoubleOrHalveSelectedStats:
  6212. callab DoubleSelectedStats
  6213. jpab HalveSelectedStats
  6214. ScrollTrainerPicAfterBattle:
  6215. jpab _ScrollTrainerPicAfterBattle
  6216. ApplyBurnAndParalysisPenaltiesToPlayer:
  6217. ld a, $1
  6218. jr ApplyBurnAndParalysisPenalties
  6219. ApplyBurnAndParalysisPenaltiesToEnemy:
  6220. xor a
  6221. ApplyBurnAndParalysisPenalties:
  6222. ld [H_WHOSETURN], a
  6223. call QuarterSpeedDueToParalysis
  6224. jp HalveAttackDueToBurn
  6225. QuarterSpeedDueToParalysis:
  6226. ld a, [H_WHOSETURN]
  6227. and a
  6228. jr z, .playerTurn
  6229. .enemyTurn ; quarter the player's speed
  6230. ld a, [wBattleMonStatus]
  6231. and 1 << PAR
  6232. ret z ; return if player not paralysed
  6233. ld hl, wBattleMonSpeed + 1
  6234. ld a, [hld]
  6235. ld b, a
  6236. ld a, [hl]
  6237. srl a
  6238. rr b
  6239. srl a
  6240. rr b
  6241. ld [hli], a
  6242. or b
  6243. jr nz, .storePlayerSpeed
  6244. ld b, 1 ; give the player a minimum of at least one speed point
  6245. .storePlayerSpeed
  6246. ld [hl], b
  6247. ret
  6248. .playerTurn ; quarter the enemy's speed
  6249. ld a, [wEnemyMonStatus]
  6250. and 1 << PAR
  6251. ret z ; return if enemy not paralysed
  6252. ld hl, wEnemyMonSpeed + 1
  6253. ld a, [hld]
  6254. ld b, a
  6255. ld a, [hl]
  6256. srl a
  6257. rr b
  6258. srl a
  6259. rr b
  6260. ld [hli], a
  6261. or b
  6262. jr nz, .storeEnemySpeed
  6263. ld b, 1 ; give the enemy a minimum of at least one speed point
  6264. .storeEnemySpeed
  6265. ld [hl], b
  6266. ret
  6267. HalveAttackDueToBurn:
  6268. ld a, [H_WHOSETURN]
  6269. and a
  6270. jr z, .playerTurn
  6271. .enemyTurn ; halve the player's attack
  6272. ld a, [wBattleMonStatus]
  6273. and 1 << BRN
  6274. ret z ; return if player not burnt
  6275. ld hl, wBattleMonAttack + 1
  6276. ld a, [hld]
  6277. ld b, a
  6278. ld a, [hl]
  6279. srl a
  6280. rr b
  6281. ld [hli], a
  6282. or b
  6283. jr nz, .storePlayerAttack
  6284. ld b, 1 ; give the player a minimum of at least one attack point
  6285. .storePlayerAttack
  6286. ld [hl], b
  6287. ret
  6288. .playerTurn ; halve the enemy's attack
  6289. ld a, [wEnemyMonStatus]
  6290. and 1 << BRN
  6291. ret z ; return if enemy not burnt
  6292. ld hl, wEnemyMonAttack + 1
  6293. ld a, [hld]
  6294. ld b, a
  6295. ld a, [hl]
  6296. srl a
  6297. rr b
  6298. ld [hli], a
  6299. or b
  6300. jr nz, .storeEnemyAttack
  6301. ld b, 1 ; give the enemy a minimum of at least one attack point
  6302. .storeEnemyAttack
  6303. ld [hl], b
  6304. ret
  6305. CalculateModifiedStats:
  6306. ld c, 0
  6307. .loop
  6308. call CalculateModifiedStat
  6309. inc c
  6310. ld a, c
  6311. cp NUM_STATS - 1
  6312. jr nz, .loop
  6313. ret
  6314. ; calculate modified stat for stat c (0 = attack, 1 = defense, 2 = speed, 3 = special)
  6315. CalculateModifiedStat:
  6316. push bc
  6317. push bc
  6318. ld a, [wCalculateWhoseStats]
  6319. and a
  6320. ld a, c
  6321. ld hl, wBattleMonAttack
  6322. ld de, wPlayerMonUnmodifiedAttack
  6323. ld bc, wPlayerMonStatMods
  6324. jr z, .next
  6325. ld hl, wEnemyMonAttack
  6326. ld de, wEnemyMonUnmodifiedAttack
  6327. ld bc, wEnemyMonStatMods
  6328. .next
  6329. add c
  6330. ld c, a
  6331. jr nc, .noCarry1
  6332. inc b
  6333. .noCarry1
  6334. ld a, [bc]
  6335. pop bc
  6336. ld b, a
  6337. push bc
  6338. sla c
  6339. ld b, 0
  6340. add hl, bc
  6341. ld a, c
  6342. add e
  6343. ld e, a
  6344. jr nc, .noCarry2
  6345. inc d
  6346. .noCarry2
  6347. pop bc
  6348. push hl
  6349. ld hl, StatModifierRatios
  6350. dec b
  6351. sla b
  6352. ld c, b
  6353. ld b, 0
  6354. add hl, bc
  6355. xor a
  6356. ld [H_MULTIPLICAND], a
  6357. ld a, [de]
  6358. ld [H_MULTIPLICAND + 1], a
  6359. inc de
  6360. ld a, [de]
  6361. ld [H_MULTIPLICAND + 2], a
  6362. ld a, [hli]
  6363. ld [H_MULTIPLIER], a
  6364. call Multiply
  6365. ld a, [hl]
  6366. ld [H_DIVISOR], a
  6367. ld b, $4
  6368. call Divide
  6369. pop hl
  6370. ld a, [H_DIVIDEND + 3]
  6371. sub 999 % $100
  6372. ld a, [H_DIVIDEND + 2]
  6373. sbc 999 / $100
  6374. jp c, .storeNewStatValue
  6375. ; cap the stat at 999
  6376. ld a, 999 / $100
  6377. ld [H_DIVIDEND + 2], a
  6378. ld a, 999 % $100
  6379. ld [H_DIVIDEND + 3], a
  6380. .storeNewStatValue
  6381. ld a, [H_DIVIDEND + 2]
  6382. ld [hli], a
  6383. ld b, a
  6384. ld a, [H_DIVIDEND + 3]
  6385. ld [hl], a
  6386. or b
  6387. jr nz, .done
  6388. inc [hl] ; if the stat is 0, bump it up to 1
  6389. .done
  6390. pop bc
  6391. ret
  6392. ApplyBadgeStatBoosts:
  6393. ld a, [wLinkState]
  6394. cp LINK_STATE_BATTLING
  6395. ret z ; return if link battle
  6396. ld a, [wObtainedBadges]
  6397. ld b, a
  6398. ld hl, wBattleMonAttack
  6399. ld c, $4
  6400. ; the boost is applied for badges whose bit position is even
  6401. ; the order of boosts matches the order they are laid out in RAM
  6402. ; Boulder (bit 0) - attack
  6403. ; Thunder (bit 2) - defense
  6404. ; Soul (bit 4) - speed
  6405. ; Volcano (bit 6) - special
  6406. .loop
  6407. srl b
  6408. call c, .applyBoostToStat
  6409. inc hl
  6410. inc hl
  6411. srl b
  6412. dec c
  6413. jr nz, .loop
  6414. ret
  6415. ; multiply stat at hl by 1.125
  6416. ; cap stat at 999
  6417. .applyBoostToStat
  6418. ld a, [hli]
  6419. ld d, a
  6420. ld e, [hl]
  6421. srl d
  6422. rr e
  6423. srl d
  6424. rr e
  6425. srl d
  6426. rr e
  6427. ld a, [hl]
  6428. add e
  6429. ld [hld], a
  6430. ld a, [hl]
  6431. adc d
  6432. ld [hli], a
  6433. ld a, [hld]
  6434. sub 999 % $100
  6435. ld a, [hl]
  6436. sbc 999 / $100
  6437. ret c
  6438. ld a, 999 / $100
  6439. ld [hli], a
  6440. ld a, 999 % $100
  6441. ld [hld], a
  6442. ret
  6443. LoadHudAndHpBarAndStatusTilePatterns:
  6444. call LoadHpBarAndStatusTilePatterns
  6445. LoadHudTilePatterns:
  6446. ld a, [rLCDC]
  6447. add a ; is LCD disabled?
  6448. jr c, .lcdEnabled
  6449. .lcdDisabled
  6450. ld hl, BattleHudTiles1
  6451. ld de, vChars2 + $6d0
  6452. ld bc, BattleHudTiles1End - BattleHudTiles1
  6453. ld a, BANK(BattleHudTiles1)
  6454. call FarCopyDataDouble
  6455. ld hl, BattleHudTiles2
  6456. ld de, vChars2 + $730
  6457. ld bc, BattleHudTiles3End - BattleHudTiles2
  6458. ld a, BANK(BattleHudTiles2)
  6459. jp FarCopyDataDouble
  6460. .lcdEnabled
  6461. ld de, BattleHudTiles1
  6462. ld hl, vChars2 + $6d0
  6463. lb bc, BANK(BattleHudTiles1), (BattleHudTiles1End - BattleHudTiles1) / $8
  6464. call CopyVideoDataDouble
  6465. ld de, BattleHudTiles2
  6466. ld hl, vChars2 + $730
  6467. lb bc, BANK(BattleHudTiles2), (BattleHudTiles3End - BattleHudTiles2) / $8
  6468. jp CopyVideoDataDouble
  6469. PrintEmptyString:
  6470. ld hl, .emptyString
  6471. jp PrintText
  6472. .emptyString
  6473. db "@"
  6474. BattleRandom:
  6475. ; Link battles use a shared PRNG.
  6476. ld a, [wLinkState]
  6477. cp LINK_STATE_BATTLING
  6478. jp nz, Random
  6479. push hl
  6480. push bc
  6481. ld a, [wLinkBattleRandomNumberListIndex]
  6482. ld c, a
  6483. ld b, 0
  6484. ld hl, wLinkBattleRandomNumberList
  6485. add hl, bc
  6486. inc a
  6487. ld [wLinkBattleRandomNumberListIndex], a
  6488. cp 9
  6489. ld a, [hl]
  6490. pop bc
  6491. pop hl
  6492. ret c
  6493. ; if we picked the last seed, we need to recalculate the nine seeds
  6494. push hl
  6495. push bc
  6496. push af
  6497. ; point to seed 0 so we pick the first number the next time
  6498. xor a
  6499. ld [wLinkBattleRandomNumberListIndex], a
  6500. ld hl, wLinkBattleRandomNumberList
  6501. ld b, 9
  6502. .loop
  6503. ld a, [hl]
  6504. ld c, a
  6505. ; multiply by 5
  6506. add a
  6507. add a
  6508. add c
  6509. ; add 1
  6510. inc a
  6511. ld [hli], a
  6512. dec b
  6513. jr nz, .loop
  6514. pop af
  6515. pop bc
  6516. pop hl
  6517. ret
  6518. HandleExplodingAnimation:
  6519. ld a, [H_WHOSETURN]
  6520. and a
  6521. ld hl, wEnemyMonType1
  6522. ld de, wEnemyBattleStatus1
  6523. ld a, [wPlayerMoveNum]
  6524. jr z, .player
  6525. ld hl, wBattleMonType1
  6526. ld de, wEnemyBattleStatus1
  6527. ld a, [wEnemyMoveNum]
  6528. .player
  6529. cp SELFDESTRUCT
  6530. jr z, .isExplodingMove
  6531. cp EXPLOSION
  6532. ret nz
  6533. .isExplodingMove
  6534. ld a, [de]
  6535. bit INVULNERABLE, a ; fly/dig
  6536. ret nz
  6537. ld a, [hli]
  6538. cp GHOST
  6539. ret z
  6540. ld a, [hl]
  6541. cp GHOST
  6542. ret z
  6543. ld a, [wMoveMissed]
  6544. and a
  6545. ret nz
  6546. ld a, 5
  6547. ld [wAnimationType], a
  6548. PlayMoveAnimation:
  6549. ld [wAnimationID], a
  6550. call Delay3
  6551. predef_jump MoveAnimation
  6552. InitBattle:
  6553. ld a, [wCurOpponent]
  6554. and a
  6555. jr z, DetermineWildOpponent
  6556. InitOpponent:
  6557. ld a, [wCurOpponent]
  6558. ld [wcf91], a
  6559. ld [wEnemyMonSpecies2], a
  6560. jr InitBattleCommon
  6561. DetermineWildOpponent:
  6562. ld a, [wd732]
  6563. bit 1, a
  6564. jr z, .asm_3ef2f
  6565. ld a, [hJoyHeld]
  6566. bit 1, a ; B button pressed?
  6567. ret nz
  6568. .asm_3ef2f
  6569. ld a, [wNumberOfNoRandomBattleStepsLeft]
  6570. and a
  6571. ret nz
  6572. callab TryDoWildEncounter
  6573. ret nz
  6574. InitBattleCommon:
  6575. ld a, [wMapPalOffset]
  6576. push af
  6577. ld hl, wLetterPrintingDelayFlags
  6578. ld a, [hl]
  6579. push af
  6580. res 1, [hl]
  6581. callab InitBattleVariables
  6582. ld a, [wEnemyMonSpecies2]
  6583. sub 200
  6584. jp c, InitWildBattle
  6585. ld [wTrainerClass], a
  6586. call GetTrainerInformation
  6587. callab ReadTrainer
  6588. call DoBattleTransitionAndInitBattleVariables
  6589. call _LoadTrainerPic
  6590. xor a
  6591. ld [wEnemyMonSpecies2], a
  6592. ld [hStartTileID], a
  6593. dec a
  6594. ld [wAICount], a
  6595. coord hl, 12, 0
  6596. predef CopyUncompressedPicToTilemap
  6597. ld a, $ff
  6598. ld [wEnemyMonPartyPos], a
  6599. ld a, $2
  6600. ld [wIsInBattle], a
  6601. jp _InitBattleCommon
  6602. InitWildBattle:
  6603. ld a, $1
  6604. ld [wIsInBattle], a
  6605. call LoadEnemyMonData
  6606. call DoBattleTransitionAndInitBattleVariables
  6607. ld a, [wCurOpponent]
  6608. cp MAROWAK
  6609. jr z, .isGhost
  6610. call IsGhostBattle
  6611. jr nz, .isNoGhost
  6612. .isGhost
  6613. ld hl, wMonHSpriteDim
  6614. ld a, $66
  6615. ld [hli], a ; write sprite dimensions
  6616. ld bc, GhostPic
  6617. ld a, c
  6618. ld [hli], a ; write front sprite pointer
  6619. ld [hl], b
  6620. ld hl, wEnemyMonNick ; set name to "GHOST"
  6621. ld a, "G"
  6622. ld [hli], a
  6623. ld a, "H"
  6624. ld [hli], a
  6625. ld a, "O"
  6626. ld [hli], a
  6627. ld a, "S"
  6628. ld [hli], a
  6629. ld a, "T"
  6630. ld [hli], a
  6631. ld [hl], "@"
  6632. ld a, [wcf91]
  6633. push af
  6634. ld a, MON_GHOST
  6635. ld [wcf91], a
  6636. ld de, vFrontPic
  6637. call LoadMonFrontSprite ; load ghost sprite
  6638. pop af
  6639. ld [wcf91], a
  6640. jr .spriteLoaded
  6641. .isNoGhost
  6642. ld de, vFrontPic
  6643. call LoadMonFrontSprite ; load mon sprite
  6644. .spriteLoaded
  6645. xor a
  6646. ld [wTrainerClass], a
  6647. ld [hStartTileID], a
  6648. coord hl, 12, 0
  6649. predef CopyUncompressedPicToTilemap
  6650. ; common code that executes after init battle code specific to trainer or wild battles
  6651. _InitBattleCommon:
  6652. ld b, SET_PAL_BATTLE_BLACK
  6653. call RunPaletteCommand
  6654. call SlidePlayerAndEnemySilhouettesOnScreen
  6655. xor a
  6656. ld [H_AUTOBGTRANSFERENABLED], a
  6657. ld hl, .emptyString
  6658. call PrintText
  6659. call SaveScreenTilesToBuffer1
  6660. call ClearScreen
  6661. ld a, $98
  6662. ld [H_AUTOBGTRANSFERDEST + 1], a
  6663. ld a, $1
  6664. ld [H_AUTOBGTRANSFERENABLED], a
  6665. call Delay3
  6666. ld a, $9c
  6667. ld [H_AUTOBGTRANSFERDEST + 1], a
  6668. call LoadScreenTilesFromBuffer1
  6669. coord hl, 9, 7
  6670. lb bc, 5, 10
  6671. call ClearScreenArea
  6672. coord hl, 1, 0
  6673. lb bc, 4, 10
  6674. call ClearScreenArea
  6675. call ClearSprites
  6676. ld a, [wIsInBattle]
  6677. dec a ; is it a wild battle?
  6678. call z, DrawEnemyHUDAndHPBar ; draw enemy HUD and HP bar if it's a wild battle
  6679. call StartBattle
  6680. callab EndOfBattle
  6681. pop af
  6682. ld [wLetterPrintingDelayFlags], a
  6683. pop af
  6684. ld [wMapPalOffset], a
  6685. ld a, [wSavedTilesetType]
  6686. ld [hTilesetType], a
  6687. scf
  6688. ret
  6689. .emptyString
  6690. db "@"
  6691. _LoadTrainerPic:
  6692. ; wd033-wd034 contain pointer to pic
  6693. ld a, [wTrainerPicPointer]
  6694. ld e, a
  6695. ld a, [wTrainerPicPointer + 1]
  6696. ld d, a ; de contains pointer to trainer pic
  6697. ld a, [wLinkState]
  6698. and a
  6699. ld a, Bank(TrainerPics) ; this is where all the trainer pics are (not counting Red's)
  6700. jr z, .loadSprite
  6701. ld a, Bank(RedPicFront)
  6702. .loadSprite
  6703. call UncompressSpriteFromDE
  6704. ld de, vFrontPic
  6705. ld a, $77
  6706. ld c, a
  6707. jp LoadUncompressedSpriteData
  6708. ; unreferenced
  6709. ResetCryModifiers:
  6710. xor a
  6711. ld [wFrequencyModifier], a
  6712. ld [wTempoModifier], a
  6713. jp PlaySound
  6714. ; animates the mon "growing" out of the pokeball
  6715. AnimateSendingOutMon:
  6716. ld a, [wPredefRegisters]
  6717. ld h, a
  6718. ld a, [wPredefRegisters + 1]
  6719. ld l, a
  6720. ld a, [hStartTileID]
  6721. ld [hBaseTileID], a
  6722. ld b, $4c
  6723. ld a, [wIsInBattle]
  6724. and a
  6725. jr z, .notInBattle
  6726. add b
  6727. ld [hl], a
  6728. call Delay3
  6729. ld bc, -(SCREEN_WIDTH * 2 + 1)
  6730. add hl, bc
  6731. ld a, 1
  6732. ld [wDownscaledMonSize], a
  6733. lb bc, 3, 3
  6734. predef CopyDownscaledMonTiles
  6735. ld c, 4
  6736. call DelayFrames
  6737. ld bc, -(SCREEN_WIDTH * 2 + 1)
  6738. add hl, bc
  6739. xor a
  6740. ld [wDownscaledMonSize], a
  6741. lb bc, 5, 5
  6742. predef CopyDownscaledMonTiles
  6743. ld c, 5
  6744. call DelayFrames
  6745. ld bc, -(SCREEN_WIDTH * 2 + 1)
  6746. jr .next
  6747. .notInBattle
  6748. ld bc, -(SCREEN_WIDTH * 6 + 3)
  6749. .next
  6750. add hl, bc
  6751. ld a, [hBaseTileID]
  6752. add $31
  6753. jr CopyUncompressedPicToHL
  6754. CopyUncompressedPicToTilemap:
  6755. ld a, [wPredefRegisters]
  6756. ld h, a
  6757. ld a, [wPredefRegisters + 1]
  6758. ld l, a
  6759. ld a, [hStartTileID]
  6760. CopyUncompressedPicToHL:
  6761. lb bc, 7, 7
  6762. ld de, SCREEN_WIDTH
  6763. push af
  6764. ld a, [wSpriteFlipped]
  6765. and a
  6766. jr nz, .flipped
  6767. pop af
  6768. .loop
  6769. push bc
  6770. push hl
  6771. .innerLoop
  6772. ld [hl], a
  6773. add hl, de
  6774. inc a
  6775. dec c
  6776. jr nz, .innerLoop
  6777. pop hl
  6778. inc hl
  6779. pop bc
  6780. dec b
  6781. jr nz, .loop
  6782. ret
  6783. .flipped
  6784. push bc
  6785. ld b, 0
  6786. dec c
  6787. add hl, bc
  6788. pop bc
  6789. pop af
  6790. .flippedLoop
  6791. push bc
  6792. push hl
  6793. .flippedInnerLoop
  6794. ld [hl], a
  6795. add hl, de
  6796. inc a
  6797. dec c
  6798. jr nz, .flippedInnerLoop
  6799. pop hl
  6800. dec hl
  6801. pop bc
  6802. dec b
  6803. jr nz, .flippedLoop
  6804. ret
  6805. LoadMonBackPic:
  6806. ; Assumes the monster's attributes have
  6807. ; been loaded with GetMonHeader.
  6808. ld a, [wBattleMonSpecies2]
  6809. ld [wcf91], a
  6810. coord hl, 1, 5
  6811. ld b, 7
  6812. ld c, 8
  6813. call ClearScreenArea
  6814. ld hl, wMonHBackSprite - wMonHeader
  6815. call UncompressMonSprite
  6816. predef ScaleSpriteByTwo
  6817. ld de, vBackPic
  6818. call InterlaceMergeSpriteBuffers ; combine the two buffers to a single 2bpp sprite
  6819. ld hl, vSprites
  6820. ld de, vBackPic
  6821. ld c, (2*SPRITEBUFFERSIZE)/16 ; count of 16-byte chunks to be copied
  6822. ld a, [H_LOADEDROMBANK]
  6823. ld b, a
  6824. jp CopyVideoData
  6825. JumpMoveEffect:
  6826. call _JumpMoveEffect
  6827. ld b, $1
  6828. ret
  6829. _JumpMoveEffect:
  6830. ld a, [H_WHOSETURN]
  6831. and a
  6832. ld a, [wPlayerMoveEffect]
  6833. jr z, .next1
  6834. ld a, [wEnemyMoveEffect]
  6835. .next1
  6836. dec a ; subtract 1, there is no special effect for 00
  6837. add a ; x2, 16bit pointers
  6838. ld hl, MoveEffectPointerTable
  6839. ld b, 0
  6840. ld c, a
  6841. add hl, bc
  6842. ld a, [hli]
  6843. ld h, [hl]
  6844. ld l, a
  6845. jp hl ; jump to special effect handler
  6846. MoveEffectPointerTable:
  6847. dw SleepEffect ; unused effect
  6848. dw PoisonEffect ; POISON_SIDE_EFFECT1
  6849. dw DrainHPEffect ; DRAIN_HP_EFFECT
  6850. dw FreezeBurnParalyzeEffect ; BURN_SIDE_EFFECT1
  6851. dw FreezeBurnParalyzeEffect ; FREEZE_SIDE_EFFECT
  6852. dw FreezeBurnParalyzeEffect ; PARALYZE_SIDE_EFFECT1
  6853. dw ExplodeEffect ; EXPLODE_EFFECT
  6854. dw DrainHPEffect ; DREAM_EATER_EFFECT
  6855. dw $0000 ; MIRROR_MOVE_EFFECT
  6856. dw StatModifierUpEffect ; ATTACK_UP1_EFFECT
  6857. dw StatModifierUpEffect ; DEFENSE_UP1_EFFECT
  6858. dw StatModifierUpEffect ; SPEED_UP1_EFFECT
  6859. dw StatModifierUpEffect ; SPECIAL_UP1_EFFECT
  6860. dw StatModifierUpEffect ; ACCURACY_UP1_EFFECT
  6861. dw StatModifierUpEffect ; EVASION_UP1_EFFECT
  6862. dw PayDayEffect ; PAY_DAY_EFFECT
  6863. dw $0000 ; SWIFT_EFFECT
  6864. dw StatModifierDownEffect ; ATTACK_DOWN1_EFFECT
  6865. dw StatModifierDownEffect ; DEFENSE_DOWN1_EFFECT
  6866. dw StatModifierDownEffect ; SPEED_DOWN1_EFFECT
  6867. dw StatModifierDownEffect ; SPECIAL_DOWN1_EFFECT
  6868. dw StatModifierDownEffect ; ACCURACY_DOWN1_EFFECT
  6869. dw StatModifierDownEffect ; EVASION_DOWN1_EFFECT
  6870. dw ConversionEffect ; CONVERSION_EFFECT
  6871. dw HazeEffect ; HAZE_EFFECT
  6872. dw BideEffect ; BIDE_EFFECT
  6873. dw ThrashPetalDanceEffect ; THRASH_PETAL_DANCE_EFFECT
  6874. dw SwitchAndTeleportEffect ; SWITCH_AND_TELEPORT_EFFECT
  6875. dw TwoToFiveAttacksEffect ; TWO_TO_FIVE_ATTACKS_EFFECT
  6876. dw TwoToFiveAttacksEffect ; unused effect
  6877. dw FlinchSideEffect ; FLINCH_SIDE_EFFECT1
  6878. dw SleepEffect ; SLEEP_EFFECT
  6879. dw PoisonEffect ; POISON_SIDE_EFFECT2
  6880. dw FreezeBurnParalyzeEffect ; BURN_SIDE_EFFECT2
  6881. dw FreezeBurnParalyzeEffect ; unused effect
  6882. dw FreezeBurnParalyzeEffect ; PARALYZE_SIDE_EFFECT2
  6883. dw FlinchSideEffect ; FLINCH_SIDE_EFFECT2
  6884. dw OneHitKOEffect ; OHKO_EFFECT
  6885. dw ChargeEffect ; CHARGE_EFFECT
  6886. dw $0000 ; SUPER_FANG_EFFECT
  6887. dw $0000 ; SPECIAL_DAMAGE_EFFECT
  6888. dw TrappingEffect ; TRAPPING_EFFECT
  6889. dw ChargeEffect ; FLY_EFFECT
  6890. dw TwoToFiveAttacksEffect ; ATTACK_TWICE_EFFECT
  6891. dw $0000 ; JUMP_KICK_EFFECT
  6892. dw MistEffect ; MIST_EFFECT
  6893. dw FocusEnergyEffect ; FOCUS_ENERGY_EFFECT
  6894. dw RecoilEffect ; RECOIL_EFFECT
  6895. dw ConfusionEffect ; CONFUSION_EFFECT
  6896. dw StatModifierUpEffect ; ATTACK_UP2_EFFECT
  6897. dw StatModifierUpEffect ; DEFENSE_UP2_EFFECT
  6898. dw StatModifierUpEffect ; SPEED_UP2_EFFECT
  6899. dw StatModifierUpEffect ; SPECIAL_UP2_EFFECT
  6900. dw StatModifierUpEffect ; ACCURACY_UP2_EFFECT
  6901. dw StatModifierUpEffect ; EVASION_UP2_EFFECT
  6902. dw HealEffect ; HEAL_EFFECT
  6903. dw TransformEffect ; TRANSFORM_EFFECT
  6904. dw StatModifierDownEffect ; ATTACK_DOWN2_EFFECT
  6905. dw StatModifierDownEffect ; DEFENSE_DOWN2_EFFECT
  6906. dw StatModifierDownEffect ; SPEED_DOWN2_EFFECT
  6907. dw StatModifierDownEffect ; SPECIAL_DOWN2_EFFECT
  6908. dw StatModifierDownEffect ; ACCURACY_DOWN2_EFFECT
  6909. dw StatModifierDownEffect ; EVASION_DOWN2_EFFECT
  6910. dw ReflectLightScreenEffect ; LIGHT_SCREEN_EFFECT
  6911. dw ReflectLightScreenEffect ; REFLECT_EFFECT
  6912. dw PoisonEffect ; POISON_EFFECT
  6913. dw ParalyzeEffect ; PARALYZE_EFFECT
  6914. dw StatModifierDownEffect ; ATTACK_DOWN_SIDE_EFFECT
  6915. dw StatModifierDownEffect ; DEFENSE_DOWN_SIDE_EFFECT
  6916. dw StatModifierDownEffect ; SPEED_DOWN_SIDE_EFFECT
  6917. dw StatModifierDownEffect ; SPECIAL_DOWN_SIDE_EFFECT
  6918. dw StatModifierDownEffect ; unused effect
  6919. dw StatModifierDownEffect ; unused effect
  6920. dw StatModifierDownEffect ; unused effect
  6921. dw StatModifierDownEffect ; unused effect
  6922. dw ConfusionSideEffect ; CONFUSION_SIDE_EFFECT
  6923. dw TwoToFiveAttacksEffect ; TWINEEDLE_EFFECT
  6924. dw $0000 ; unused effect
  6925. dw SubstituteEffect ; SUBSTITUTE_EFFECT
  6926. dw HyperBeamEffect ; HYPER_BEAM_EFFECT
  6927. dw RageEffect ; RAGE_EFFECT
  6928. dw MimicEffect ; MIMIC_EFFECT
  6929. dw $0000 ; METRONOME_EFFECT
  6930. dw LeechSeedEffect ; LEECH_SEED_EFFECT
  6931. dw SplashEffect ; SPLASH_EFFECT
  6932. dw DisableEffect ; DISABLE_EFFECT
  6933. SleepEffect:
  6934. ld de, wEnemyMonStatus
  6935. ld bc, wEnemyBattleStatus2
  6936. ld a, [H_WHOSETURN]
  6937. and a
  6938. jp z, .sleepEffect
  6939. ld de, wBattleMonStatus
  6940. ld bc, wPlayerBattleStatus2
  6941. .sleepEffect
  6942. ld a, [bc]
  6943. bit NEEDS_TO_RECHARGE, a ; does the target need to recharge? (hyper beam)
  6944. res NEEDS_TO_RECHARGE, a ; target no longer needs to recharge
  6945. ld [bc], a
  6946. jr nz, .setSleepCounter ; if the target had to recharge, all hit tests will be skipped
  6947. ; including the event where the target already has another status
  6948. ld a, [de]
  6949. ld b, a
  6950. and $7
  6951. jr z, .notAlreadySleeping ; can't affect a mon that is already asleep
  6952. ld hl, AlreadyAsleepText
  6953. jp PrintText
  6954. .notAlreadySleeping
  6955. ld a, b
  6956. and a
  6957. jr nz, .didntAffect ; can't affect a mon that is already statused
  6958. push de
  6959. call MoveHitTest ; apply accuracy tests
  6960. pop de
  6961. ld a, [wMoveMissed]
  6962. and a
  6963. jr nz, .didntAffect
  6964. .setSleepCounter
  6965. ; set target's sleep counter to a random number between 1 and 7
  6966. call BattleRandom
  6967. and $7
  6968. jr z, .setSleepCounter
  6969. ld [de], a
  6970. call PlayCurrentMoveAnimation2
  6971. ld hl, FellAsleepText
  6972. jp PrintText
  6973. .didntAffect
  6974. jp PrintDidntAffectText
  6975. FellAsleepText:
  6976. TX_FAR _FellAsleepText
  6977. db "@"
  6978. AlreadyAsleepText:
  6979. TX_FAR _AlreadyAsleepText
  6980. db "@"
  6981. PoisonEffect:
  6982. ld hl, wEnemyMonStatus
  6983. ld de, wPlayerMoveEffect
  6984. ld a, [H_WHOSETURN]
  6985. and a
  6986. jr z, .poisonEffect
  6987. ld hl, wBattleMonStatus
  6988. ld de, wEnemyMoveEffect
  6989. .poisonEffect
  6990. call CheckTargetSubstitute
  6991. jr nz, .noEffect ; can't poison a substitute target
  6992. ld a, [hli]
  6993. ld b, a
  6994. and a
  6995. jr nz, .noEffect ; miss if target is already statused
  6996. ld a, [hli]
  6997. cp POISON ; can't poison a poison-type target
  6998. jr z, .noEffect
  6999. ld a, [hld]
  7000. cp POISON ; can't poison a poison-type target
  7001. jr z, .noEffect
  7002. ld a, [de]
  7003. cp POISON_SIDE_EFFECT1
  7004. ld b, $34 ; ~20% chance of poisoning
  7005. jr z, .sideEffectTest
  7006. cp POISON_SIDE_EFFECT2
  7007. ld b, $67 ; ~40% chance of poisoning
  7008. jr z, .sideEffectTest
  7009. push hl
  7010. push de
  7011. call MoveHitTest ; apply accuracy tests
  7012. pop de
  7013. pop hl
  7014. ld a, [wMoveMissed]
  7015. and a
  7016. jr nz, .didntAffect
  7017. jr .inflictPoison
  7018. .sideEffectTest
  7019. call BattleRandom
  7020. cp b ; was side effect successful?
  7021. ret nc
  7022. .inflictPoison
  7023. dec hl
  7024. set 3, [hl] ; mon is now poisoned
  7025. push de
  7026. dec de
  7027. ld a, [H_WHOSETURN]
  7028. and a
  7029. ld b, ANIM_C7
  7030. ld hl, wPlayerBattleStatus3
  7031. ld a, [de]
  7032. ld de, wPlayerToxicCounter
  7033. jr nz, .ok
  7034. ld b, ANIM_A9
  7035. ld hl, wEnemyBattleStatus3
  7036. ld de, wEnemyToxicCounter
  7037. .ok
  7038. cp TOXIC
  7039. jr nz, .normalPoison ; done if move is not Toxic
  7040. set BADLY_POISONED, [hl] ; else set Toxic battstatus
  7041. xor a
  7042. ld [de], a
  7043. ld hl, BadlyPoisonedText
  7044. jr .continue
  7045. .normalPoison
  7046. ld hl, PoisonedText
  7047. .continue
  7048. pop de
  7049. ld a, [de]
  7050. cp POISON_EFFECT
  7051. jr z, .regularPoisonEffect
  7052. ld a, b
  7053. call PlayBattleAnimation2
  7054. jp PrintText
  7055. .regularPoisonEffect
  7056. call PlayCurrentMoveAnimation2
  7057. jp PrintText
  7058. .noEffect
  7059. ld a, [de]
  7060. cp POISON_EFFECT
  7061. ret nz
  7062. .didntAffect
  7063. ld c, 50
  7064. call DelayFrames
  7065. jp PrintDidntAffectText
  7066. PoisonedText:
  7067. TX_FAR _PoisonedText
  7068. db "@"
  7069. BadlyPoisonedText:
  7070. TX_FAR _BadlyPoisonedText
  7071. db "@"
  7072. DrainHPEffect:
  7073. jpab DrainHPEffect_
  7074. ExplodeEffect:
  7075. ld hl, wBattleMonHP
  7076. ld de, wPlayerBattleStatus2
  7077. ld a, [H_WHOSETURN]
  7078. and a
  7079. jr z, .faintUser
  7080. ld hl, wEnemyMonHP
  7081. ld de, wEnemyBattleStatus2
  7082. .faintUser
  7083. xor a
  7084. ld [hli], a ; set the mon's HP to 0
  7085. ld [hli], a
  7086. inc hl
  7087. ld [hl], a ; set mon's status to 0
  7088. ld a, [de]
  7089. res SEEDED, a ; clear mon's leech seed status
  7090. ld [de], a
  7091. ret
  7092. FreezeBurnParalyzeEffect:
  7093. xor a
  7094. ld [wAnimationType], a
  7095. call CheckTargetSubstitute ; test bit 4 of d063/d068 flags [target has substitute flag]
  7096. ret nz ; return if they have a substitute, can't effect them
  7097. ld a, [H_WHOSETURN]
  7098. and a
  7099. jp nz, opponentAttacker
  7100. ld a, [wEnemyMonStatus]
  7101. and a
  7102. jp nz, CheckDefrost ; can't inflict status if opponent is already statused
  7103. ld a, [wPlayerMoveType]
  7104. ld b, a
  7105. ld a, [wEnemyMonType1]
  7106. cp b ; do target type 1 and move type match?
  7107. ret z ; return if they match (an ice move can't freeze an ice-type, body slam can't paralyze a normal-type, etc.)
  7108. ld a, [wEnemyMonType2]
  7109. cp b ; do target type 2 and move type match?
  7110. ret z ; return if they match
  7111. ld a, [wPlayerMoveEffect]
  7112. cp PARALYZE_SIDE_EFFECT1 + 1 ; 10% status effects are 04, 05, 06 so 07 will set carry for those
  7113. ld b, $1a ; 0x1A/0x100 or 26/256 = 10.2%~ chance
  7114. jr c, .next1 ; branch ahead if this is a 10% chance effect..
  7115. ld b, $4d ; else use 0x4D/0x100 or 77/256 = 30.1%~ chance
  7116. sub $1e ; subtract $1E to map to equivalent 10% chance effects
  7117. .next1
  7118. push af
  7119. call BattleRandom ; get random 8bit value for probability test
  7120. cp b
  7121. pop bc
  7122. ret nc ; do nothing if random value is >= 1A or 4D [no status applied]
  7123. ld a, b ; what type of effect is this?
  7124. cp BURN_SIDE_EFFECT1
  7125. jr z, .burn
  7126. cp FREEZE_SIDE_EFFECT
  7127. jr z, .freeze
  7128. ; .paralyze
  7129. ld a, 1 << PAR
  7130. ld [wEnemyMonStatus], a
  7131. call QuarterSpeedDueToParalysis ; quarter speed of affected mon
  7132. ld a, ANIM_A9
  7133. call PlayBattleAnimation
  7134. jp PrintMayNotAttackText ; print paralysis text
  7135. .burn
  7136. ld a, 1 << BRN
  7137. ld [wEnemyMonStatus], a
  7138. call HalveAttackDueToBurn ; halve attack of affected mon
  7139. ld a, ANIM_A9
  7140. call PlayBattleAnimation
  7141. ld hl, BurnedText
  7142. jp PrintText
  7143. .freeze
  7144. call ClearHyperBeam ; resets hyper beam (recharge) condition from target
  7145. ld a, 1 << FRZ
  7146. ld [wEnemyMonStatus], a
  7147. ld a, ANIM_A9
  7148. call PlayBattleAnimation
  7149. ld hl, FrozenText
  7150. jp PrintText
  7151. opponentAttacker:
  7152. ld a, [wBattleMonStatus] ; mostly same as above with addresses swapped for opponent
  7153. and a
  7154. jp nz, CheckDefrost
  7155. ld a, [wEnemyMoveType]
  7156. ld b, a
  7157. ld a, [wBattleMonType1]
  7158. cp b
  7159. ret z
  7160. ld a, [wBattleMonType2]
  7161. cp b
  7162. ret z
  7163. ld a, [wEnemyMoveEffect]
  7164. cp PARALYZE_SIDE_EFFECT1 + 1
  7165. ld b, $1a
  7166. jr c, .next1
  7167. ld b, $4d
  7168. sub $1e
  7169. .next1
  7170. push af
  7171. call BattleRandom
  7172. cp b
  7173. pop bc
  7174. ret nc
  7175. ld a, b
  7176. cp BURN_SIDE_EFFECT1
  7177. jr z, .burn
  7178. cp FREEZE_SIDE_EFFECT
  7179. jr z, .freeze
  7180. ld a, 1 << PAR
  7181. ld [wBattleMonStatus], a
  7182. call QuarterSpeedDueToParalysis
  7183. jp PrintMayNotAttackText
  7184. .burn
  7185. ld a, 1 << BRN
  7186. ld [wBattleMonStatus], a
  7187. call HalveAttackDueToBurn
  7188. ld hl, BurnedText
  7189. jp PrintText
  7190. .freeze
  7191. ; hyper beam bits aren't reseted for opponent's side
  7192. ld a, 1 << FRZ
  7193. ld [wBattleMonStatus], a
  7194. ld hl, FrozenText
  7195. jp PrintText
  7196. BurnedText:
  7197. TX_FAR _BurnedText
  7198. db "@"
  7199. FrozenText:
  7200. TX_FAR _FrozenText
  7201. db "@"
  7202. CheckDefrost:
  7203. ; any fire-type move that has a chance inflict burn (all but Fire Spin) will defrost a frozen target
  7204. and 1 << FRZ ; are they frozen?
  7205. ret z ; return if so
  7206. ld a, [H_WHOSETURN]
  7207. and a
  7208. jr nz, .opponent
  7209. ;player [attacker]
  7210. ld a, [wPlayerMoveType]
  7211. sub FIRE
  7212. ret nz ; return if type of move used isn't fire
  7213. ld [wEnemyMonStatus], a ; set opponent status to 00 ["defrost" a frozen monster]
  7214. ld hl, wEnemyMon1Status
  7215. ld a, [wEnemyMonPartyPos]
  7216. ld bc, wEnemyMon2 - wEnemyMon1
  7217. call AddNTimes
  7218. xor a
  7219. ld [hl], a ; clear status in roster
  7220. ld hl, FireDefrostedText
  7221. jr .common
  7222. .opponent
  7223. ld a, [wEnemyMoveType] ; same as above with addresses swapped
  7224. sub FIRE
  7225. ret nz
  7226. ld [wBattleMonStatus], a
  7227. ld hl, wPartyMon1Status
  7228. ld a, [wPlayerMonNumber]
  7229. ld bc, wPartyMon2 - wPartyMon1
  7230. call AddNTimes
  7231. xor a
  7232. ld [hl], a
  7233. ld hl, FireDefrostedText
  7234. .common
  7235. jp PrintText
  7236. FireDefrostedText:
  7237. TX_FAR _FireDefrostedText
  7238. db "@"
  7239. StatModifierUpEffect:
  7240. ld hl, wPlayerMonStatMods
  7241. ld de, wPlayerMoveEffect
  7242. ld a, [H_WHOSETURN]
  7243. and a
  7244. jr z, .statModifierUpEffect
  7245. ld hl, wEnemyMonStatMods
  7246. ld de, wEnemyMoveEffect
  7247. .statModifierUpEffect
  7248. ld a, [de]
  7249. sub ATTACK_UP1_EFFECT
  7250. cp EVASION_UP1_EFFECT + $3 - ATTACK_UP1_EFFECT ; covers all +1 effects
  7251. jr c, .incrementStatMod
  7252. sub ATTACK_UP2_EFFECT - ATTACK_UP1_EFFECT ; map +2 effects to equivalent +1 effect
  7253. .incrementStatMod
  7254. ld c, a
  7255. ld b, $0
  7256. add hl, bc
  7257. ld b, [hl]
  7258. inc b ; increment corresponding stat mod
  7259. ld a, $d
  7260. cp b ; can't raise stat past +6 ($d or 13)
  7261. jp c, PrintNothingHappenedText
  7262. ld a, [de]
  7263. cp ATTACK_UP1_EFFECT + $8 ; is it a +2 effect?
  7264. jr c, .ok
  7265. inc b ; if so, increment stat mod again
  7266. ld a, $d
  7267. cp b ; unless it's already +6
  7268. jr nc, .ok
  7269. ld b, a
  7270. .ok
  7271. ld [hl], b
  7272. ld a, c
  7273. cp $4
  7274. jr nc, UpdateStatDone ; jump if mod affected is evasion/accuracy
  7275. push hl
  7276. ld hl, wBattleMonAttack + 1
  7277. ld de, wPlayerMonUnmodifiedAttack
  7278. ld a, [H_WHOSETURN]
  7279. and a
  7280. jr z, .pointToStats
  7281. ld hl, wEnemyMonAttack + 1
  7282. ld de, wEnemyMonUnmodifiedAttack
  7283. .pointToStats
  7284. push bc
  7285. sla c
  7286. ld b, $0
  7287. add hl, bc ; hl = modified stat
  7288. ld a, c
  7289. add e
  7290. ld e, a
  7291. jr nc, .checkIf999
  7292. inc d ; de = unmodified (original) stat
  7293. .checkIf999
  7294. pop bc
  7295. ld a, [hld]
  7296. sub 999 % $100 ; check if stat is already 999
  7297. jr nz, .recalculateStat
  7298. ld a, [hl]
  7299. sbc 999 / $100
  7300. jp z, RestoreOriginalStatModifier
  7301. .recalculateStat ; recalculate affected stat
  7302. ; paralysis and burn penalties, as well as badge boosts are ignored
  7303. push hl
  7304. push bc
  7305. ld hl, StatModifierRatios
  7306. dec b
  7307. sla b
  7308. ld c, b
  7309. ld b, $0
  7310. add hl, bc
  7311. pop bc
  7312. xor a
  7313. ld [H_MULTIPLICAND], a
  7314. ld a, [de]
  7315. ld [H_MULTIPLICAND + 1], a
  7316. inc de
  7317. ld a, [de]
  7318. ld [H_MULTIPLICAND + 2], a
  7319. ld a, [hli]
  7320. ld [H_MULTIPLIER], a
  7321. call Multiply
  7322. ld a, [hl]
  7323. ld [H_DIVISOR], a
  7324. ld b, $4
  7325. call Divide
  7326. pop hl
  7327. ; cap at 999
  7328. ld a, [H_PRODUCT + 3]
  7329. sub 999 % $100
  7330. ld a, [H_PRODUCT + 2]
  7331. sbc 999 / $100
  7332. jp c, UpdateStat
  7333. ld a, 999 / $100
  7334. ld [H_MULTIPLICAND + 1], a
  7335. ld a, 999 % $100
  7336. ld [H_MULTIPLICAND + 2], a
  7337. UpdateStat:
  7338. ld a, [H_PRODUCT + 2]
  7339. ld [hli], a
  7340. ld a, [H_PRODUCT + 3]
  7341. ld [hl], a
  7342. pop hl
  7343. UpdateStatDone:
  7344. ld b, c
  7345. inc b
  7346. call PrintStatText
  7347. ld hl, wPlayerBattleStatus2
  7348. ld de, wPlayerMoveNum
  7349. ld bc, wPlayerMonMinimized
  7350. ld a, [H_WHOSETURN]
  7351. and a
  7352. jr z, .asm_3f4e6
  7353. ld hl, wEnemyBattleStatus2
  7354. ld de, wEnemyMoveNum
  7355. ld bc, wEnemyMonMinimized
  7356. .asm_3f4e6
  7357. ld a, [de]
  7358. cp MINIMIZE
  7359. jr nz, .asm_3f4f9
  7360. ; if a substitute is up, slide off the substitute and show the mon pic before
  7361. ; playing the minimize animation
  7362. bit HAS_SUBSTITUTE_UP, [hl]
  7363. push af
  7364. push bc
  7365. ld hl, HideSubstituteShowMonAnim
  7366. ld b, BANK(HideSubstituteShowMonAnim)
  7367. push de
  7368. call nz, Bankswitch
  7369. pop de
  7370. .asm_3f4f9
  7371. call PlayCurrentMoveAnimation
  7372. ld a, [de]
  7373. cp MINIMIZE
  7374. jr nz, .applyBadgeBoostsAndStatusPenalties
  7375. pop bc
  7376. ld a, $1
  7377. ld [bc], a
  7378. ld hl, ReshowSubstituteAnim
  7379. ld b, BANK(ReshowSubstituteAnim)
  7380. pop af
  7381. call nz, Bankswitch
  7382. .applyBadgeBoostsAndStatusPenalties
  7383. ld a, [H_WHOSETURN]
  7384. and a
  7385. call z, ApplyBadgeStatBoosts ; whenever the player uses a stat-up move, badge boosts get reapplied again to every stat,
  7386. ; even to those not affected by the stat-up move (will be boosted further)
  7387. ld hl, MonsStatsRoseText
  7388. call PrintText
  7389. ; these shouldn't be here
  7390. call QuarterSpeedDueToParalysis ; apply speed penalty to the player whose turn is not, if it's paralyzed
  7391. jp HalveAttackDueToBurn ; apply attack penalty to the player whose turn is not, if it's burned
  7392. RestoreOriginalStatModifier:
  7393. pop hl
  7394. dec [hl]
  7395. PrintNothingHappenedText:
  7396. ld hl, NothingHappenedText
  7397. jp PrintText
  7398. MonsStatsRoseText:
  7399. TX_FAR _MonsStatsRoseText
  7400. TX_ASM
  7401. ld hl, GreatlyRoseText
  7402. ld a, [H_WHOSETURN]
  7403. and a
  7404. ld a, [wPlayerMoveEffect]
  7405. jr z, .playerTurn
  7406. ld a, [wEnemyMoveEffect]
  7407. .playerTurn
  7408. cp ATTACK_DOWN1_EFFECT
  7409. ret nc
  7410. ld hl, RoseText
  7411. ret
  7412. GreatlyRoseText:
  7413. TX_DELAY
  7414. TX_FAR _GreatlyRoseText
  7415. ; fallthrough
  7416. RoseText:
  7417. TX_FAR _RoseText
  7418. db "@"
  7419. StatModifierDownEffect:
  7420. ld hl, wEnemyMonStatMods
  7421. ld de, wPlayerMoveEffect
  7422. ld bc, wEnemyBattleStatus1
  7423. ld a, [H_WHOSETURN]
  7424. and a
  7425. jr z, .statModifierDownEffect
  7426. ld hl, wPlayerMonStatMods
  7427. ld de, wEnemyMoveEffect
  7428. ld bc, wPlayerBattleStatus1
  7429. ld a, [wLinkState]
  7430. cp LINK_STATE_BATTLING
  7431. jr z, .statModifierDownEffect
  7432. call BattleRandom
  7433. cp $40 ; 1/4 chance to miss by in regular battle
  7434. jp c, MoveMissed
  7435. .statModifierDownEffect
  7436. call CheckTargetSubstitute ; can't hit through substitute
  7437. jp nz, MoveMissed
  7438. ld a, [de]
  7439. cp ATTACK_DOWN_SIDE_EFFECT
  7440. jr c, .nonSideEffect
  7441. call BattleRandom
  7442. cp $55 ; 85/256 chance for side effects
  7443. jp nc, CantLowerAnymore
  7444. ld a, [de]
  7445. sub ATTACK_DOWN_SIDE_EFFECT ; map each stat to 0-3
  7446. jr .decrementStatMod
  7447. .nonSideEffect ; non-side effects only
  7448. push hl
  7449. push de
  7450. push bc
  7451. call MoveHitTest ; apply accuracy tests
  7452. pop bc
  7453. pop de
  7454. pop hl
  7455. ld a, [wMoveMissed]
  7456. and a
  7457. jp nz, MoveMissed
  7458. ld a, [bc]
  7459. bit INVULNERABLE, a ; fly/dig
  7460. jp nz, MoveMissed
  7461. ld a, [de]
  7462. sub ATTACK_DOWN1_EFFECT
  7463. cp EVASION_DOWN1_EFFECT + $3 - ATTACK_DOWN1_EFFECT ; covers all -1 effects
  7464. jr c, .decrementStatMod
  7465. sub ATTACK_DOWN2_EFFECT - ATTACK_DOWN1_EFFECT ; map -2 effects to corresponding -1 effect
  7466. .decrementStatMod
  7467. ld c, a
  7468. ld b, $0
  7469. add hl, bc
  7470. ld b, [hl]
  7471. dec b ; dec corresponding stat mod
  7472. jp z, CantLowerAnymore ; if stat mod is 1 (-6), can't lower anymore
  7473. ld a, [de]
  7474. cp ATTACK_DOWN2_EFFECT - $16 ; $24
  7475. jr c, .ok
  7476. cp EVASION_DOWN2_EFFECT + $5 ; $44
  7477. jr nc, .ok
  7478. dec b ; stat down 2 effects only (dec mod again)
  7479. jr nz, .ok
  7480. inc b ; increment mod to 1 (-6) if it would become 0 (-7)
  7481. .ok
  7482. ld [hl], b ; save modified mod
  7483. ld a, c
  7484. cp $4
  7485. jr nc, UpdateLoweredStatDone ; jump for evasion/accuracy
  7486. push hl
  7487. push de
  7488. ld hl, wEnemyMonAttack + 1
  7489. ld de, wEnemyMonUnmodifiedAttack
  7490. ld a, [H_WHOSETURN]
  7491. and a
  7492. jr z, .pointToStat
  7493. ld hl, wBattleMonAttack + 1
  7494. ld de, wPlayerMonUnmodifiedAttack
  7495. .pointToStat
  7496. push bc
  7497. sla c
  7498. ld b, $0
  7499. add hl, bc ; hl = modified stat
  7500. ld a, c
  7501. add e
  7502. ld e, a
  7503. jr nc, .noCarry
  7504. inc d ; de = unmodified stat
  7505. .noCarry
  7506. pop bc
  7507. ld a, [hld]
  7508. sub $1 ; can't lower stat below 1 (-6)
  7509. jr nz, .recalculateStat
  7510. ld a, [hl]
  7511. and a
  7512. jp z, CantLowerAnymore_Pop
  7513. .recalculateStat
  7514. ; recalculate affected stat
  7515. ; paralysis and burn penalties, as well as badge boosts are ignored
  7516. push hl
  7517. push bc
  7518. ld hl, StatModifierRatios
  7519. dec b
  7520. sla b
  7521. ld c, b
  7522. ld b, $0
  7523. add hl, bc
  7524. pop bc
  7525. xor a
  7526. ld [H_MULTIPLICAND], a
  7527. ld a, [de]
  7528. ld [H_MULTIPLICAND + 1], a
  7529. inc de
  7530. ld a, [de]
  7531. ld [H_MULTIPLICAND + 2], a
  7532. ld a, [hli]
  7533. ld [H_MULTIPLIER], a
  7534. call Multiply
  7535. ld a, [hl]
  7536. ld [H_DIVISOR], a
  7537. ld b, $4
  7538. call Divide
  7539. pop hl
  7540. ld a, [H_PRODUCT + 3]
  7541. ld b, a
  7542. ld a, [H_PRODUCT + 2]
  7543. or b
  7544. jp nz, UpdateLoweredStat
  7545. ld [H_MULTIPLICAND + 1], a
  7546. ld a, $1
  7547. ld [H_MULTIPLICAND + 2], a
  7548. UpdateLoweredStat:
  7549. ld a, [H_PRODUCT + 2]
  7550. ld [hli], a
  7551. ld a, [H_PRODUCT + 3]
  7552. ld [hl], a
  7553. pop de
  7554. pop hl
  7555. UpdateLoweredStatDone:
  7556. ld b, c
  7557. inc b
  7558. push de
  7559. call PrintStatText
  7560. pop de
  7561. ld a, [de]
  7562. cp $44
  7563. jr nc, .ApplyBadgeBoostsAndStatusPenalties
  7564. call PlayCurrentMoveAnimation2
  7565. .ApplyBadgeBoostsAndStatusPenalties
  7566. ld a, [H_WHOSETURN]
  7567. and a
  7568. call nz, ApplyBadgeStatBoosts ; whenever the player uses a stat-down move, badge boosts get reapplied again to every stat,
  7569. ; even to those not affected by the stat-up move (will be boosted further)
  7570. ld hl, MonsStatsFellText
  7571. call PrintText
  7572. ; These where probably added given that a stat-down move affecting speed or attack will override
  7573. ; the stat penalties from paralysis and burn respectively.
  7574. ; But they are always called regardless of the stat affected by the stat-down move.
  7575. call QuarterSpeedDueToParalysis
  7576. jp HalveAttackDueToBurn
  7577. CantLowerAnymore_Pop:
  7578. pop de
  7579. pop hl
  7580. inc [hl]
  7581. CantLowerAnymore:
  7582. ld a, [de]
  7583. cp ATTACK_DOWN_SIDE_EFFECT
  7584. ret nc
  7585. ld hl, NothingHappenedText
  7586. jp PrintText
  7587. MoveMissed:
  7588. ld a, [de]
  7589. cp $44
  7590. ret nc
  7591. jp ConditionalPrintButItFailed
  7592. MonsStatsFellText:
  7593. TX_FAR _MonsStatsFellText
  7594. TX_ASM
  7595. ld hl, FellText
  7596. ld a, [H_WHOSETURN]
  7597. and a
  7598. ld a, [wPlayerMoveEffect]
  7599. jr z, .playerTurn
  7600. ld a, [wEnemyMoveEffect]
  7601. .playerTurn
  7602. ; check if the move's effect decreases a stat by 2
  7603. cp BIDE_EFFECT
  7604. ret c
  7605. cp ATTACK_DOWN_SIDE_EFFECT
  7606. ret nc
  7607. ld hl, GreatlyFellText
  7608. ret
  7609. GreatlyFellText:
  7610. TX_DELAY
  7611. TX_FAR _GreatlyFellText
  7612. ; fallthrough
  7613. FellText:
  7614. TX_FAR _FellText
  7615. db "@"
  7616. PrintStatText:
  7617. ld hl, StatsTextStrings
  7618. ld c, "@"
  7619. .findStatName_outer
  7620. dec b
  7621. jr z, .foundStatName
  7622. .findStatName_inner
  7623. ld a, [hli]
  7624. cp c
  7625. jr z, .findStatName_outer
  7626. jr .findStatName_inner
  7627. .foundStatName
  7628. ld de, wcf4b
  7629. ld bc, $a
  7630. jp CopyData
  7631. StatsTextStrings:
  7632. db "ATTACK@"
  7633. db "DEFENSE@"
  7634. db "SPEED@"
  7635. db "SPECIAL@"
  7636. db "ACCURACY@"
  7637. db "EVADE@"
  7638. StatModifierRatios:
  7639. ; first byte is numerator, second byte is denominator
  7640. db 25, 100 ; 0.25
  7641. db 28, 100 ; 0.28
  7642. db 33, 100 ; 0.33
  7643. db 40, 100 ; 0.40
  7644. db 50, 100 ; 0.50
  7645. db 66, 100 ; 0.66
  7646. db 1, 1 ; 1.00
  7647. db 15, 10 ; 1.50
  7648. db 2, 1 ; 2.00
  7649. db 25, 10 ; 2.50
  7650. db 3, 1 ; 3.00
  7651. db 35, 10 ; 3.50
  7652. db 4, 1 ; 4.00
  7653. BideEffect:
  7654. ld hl, wPlayerBattleStatus1
  7655. ld de, wPlayerBideAccumulatedDamage
  7656. ld bc, wPlayerNumAttacksLeft
  7657. ld a, [H_WHOSETURN]
  7658. and a
  7659. jr z, .bideEffect
  7660. ld hl, wEnemyBattleStatus1
  7661. ld de, wEnemyBideAccumulatedDamage
  7662. ld bc, wEnemyNumAttacksLeft
  7663. .bideEffect
  7664. set STORING_ENERGY, [hl] ; mon is now using bide
  7665. xor a
  7666. ld [de], a
  7667. inc de
  7668. ld [de], a
  7669. ld [wPlayerMoveEffect], a
  7670. ld [wEnemyMoveEffect], a
  7671. call BattleRandom
  7672. and $1
  7673. inc a
  7674. inc a
  7675. ld [bc], a ; set Bide counter to 2 or 3 at random
  7676. ld a, [H_WHOSETURN]
  7677. add XSTATITEM_ANIM
  7678. jp PlayBattleAnimation2
  7679. ThrashPetalDanceEffect:
  7680. ld hl, wPlayerBattleStatus1
  7681. ld de, wPlayerNumAttacksLeft
  7682. ld a, [H_WHOSETURN]
  7683. and a
  7684. jr z, .thrashPetalDanceEffect
  7685. ld hl, wEnemyBattleStatus1
  7686. ld de, wEnemyNumAttacksLeft
  7687. .thrashPetalDanceEffect
  7688. set THRASHING_ABOUT, [hl] ; mon is now using thrash/petal dance
  7689. call BattleRandom
  7690. and $1
  7691. inc a
  7692. inc a
  7693. ld [de], a ; set thrash/petal dance counter to 2 or 3 at random
  7694. ld a, [H_WHOSETURN]
  7695. add ANIM_B0
  7696. jp PlayBattleAnimation2
  7697. SwitchAndTeleportEffect:
  7698. ld a, [H_WHOSETURN]
  7699. and a
  7700. jr nz, .handleEnemy
  7701. ld a, [wIsInBattle]
  7702. dec a
  7703. jr nz, .notWildBattle1
  7704. ld a, [wCurEnemyLVL]
  7705. ld b, a
  7706. ld a, [wBattleMonLevel]
  7707. cp b ; is the player's level greater than the enemy's level?
  7708. jr nc, .playerMoveWasSuccessful ; if so, teleport will always succeed
  7709. add b
  7710. ld c, a
  7711. inc c ; c = sum of player level and enemy level
  7712. .rejectionSampleLoop1
  7713. call BattleRandom
  7714. cp c ; get a random number between 0 and c
  7715. jr nc, .rejectionSampleLoop1
  7716. srl b
  7717. srl b ; b = enemyLevel / 4
  7718. cp b ; is rand[0, playerLevel + enemyLevel) >= (enemyLevel / 4)?
  7719. jr nc, .playerMoveWasSuccessful ; if so, allow teleporting
  7720. ld c, 50
  7721. call DelayFrames
  7722. ld a, [wPlayerMoveNum]
  7723. cp TELEPORT
  7724. jp nz, PrintDidntAffectText
  7725. jp PrintButItFailedText_
  7726. .playerMoveWasSuccessful
  7727. call ReadPlayerMonCurHPAndStatus
  7728. xor a
  7729. ld [wAnimationType], a
  7730. inc a
  7731. ld [wEscapedFromBattle], a
  7732. ld a, [wPlayerMoveNum]
  7733. jr .playAnimAndPrintText
  7734. .notWildBattle1
  7735. ld c, 50
  7736. call DelayFrames
  7737. ld hl, IsUnaffectedText
  7738. ld a, [wPlayerMoveNum]
  7739. cp TELEPORT
  7740. jp nz, PrintText
  7741. jp PrintButItFailedText_
  7742. .handleEnemy
  7743. ld a, [wIsInBattle]
  7744. dec a
  7745. jr nz, .notWildBattle2
  7746. ld a, [wBattleMonLevel]
  7747. ld b, a
  7748. ld a, [wCurEnemyLVL]
  7749. cp b
  7750. jr nc, .enemyMoveWasSuccessful
  7751. add b
  7752. ld c, a
  7753. inc c
  7754. .rejectionSampleLoop2
  7755. call BattleRandom
  7756. cp c
  7757. jr nc, .rejectionSampleLoop2
  7758. srl b
  7759. srl b
  7760. cp b
  7761. jr nc, .enemyMoveWasSuccessful
  7762. ld c, 50
  7763. call DelayFrames
  7764. ld a, [wEnemyMoveNum]
  7765. cp TELEPORT
  7766. jp nz, PrintDidntAffectText
  7767. jp PrintButItFailedText_
  7768. .enemyMoveWasSuccessful
  7769. call ReadPlayerMonCurHPAndStatus
  7770. xor a
  7771. ld [wAnimationType], a
  7772. inc a
  7773. ld [wEscapedFromBattle], a
  7774. ld a, [wEnemyMoveNum]
  7775. jr .playAnimAndPrintText
  7776. .notWildBattle2
  7777. ld c, 50
  7778. call DelayFrames
  7779. ld hl, IsUnaffectedText
  7780. ld a, [wEnemyMoveNum]
  7781. cp TELEPORT
  7782. jp nz, PrintText
  7783. jp ConditionalPrintButItFailed
  7784. .playAnimAndPrintText
  7785. push af
  7786. call PlayBattleAnimation
  7787. ld c, 20
  7788. call DelayFrames
  7789. pop af
  7790. ld hl, RanFromBattleText
  7791. cp TELEPORT
  7792. jr z, .printText
  7793. ld hl, RanAwayScaredText
  7794. cp ROAR
  7795. jr z, .printText
  7796. ld hl, WasBlownAwayText
  7797. .printText
  7798. jp PrintText
  7799. RanFromBattleText:
  7800. TX_FAR _RanFromBattleText
  7801. db "@"
  7802. RanAwayScaredText:
  7803. TX_FAR _RanAwayScaredText
  7804. db "@"
  7805. WasBlownAwayText:
  7806. TX_FAR _WasBlownAwayText
  7807. db "@"
  7808. TwoToFiveAttacksEffect:
  7809. ld hl, wPlayerBattleStatus1
  7810. ld de, wPlayerNumAttacksLeft
  7811. ld bc, wPlayerNumHits
  7812. ld a, [H_WHOSETURN]
  7813. and a
  7814. jr z, .twoToFiveAttacksEffect
  7815. ld hl, wEnemyBattleStatus1
  7816. ld de, wEnemyNumAttacksLeft
  7817. ld bc, wEnemyNumHits
  7818. .twoToFiveAttacksEffect
  7819. bit ATTACKING_MULTIPLE_TIMES, [hl] ; is mon attacking multiple times?
  7820. ret nz
  7821. set ATTACKING_MULTIPLE_TIMES, [hl] ; mon is now attacking multiple times
  7822. ld hl, wPlayerMoveEffect
  7823. ld a, [H_WHOSETURN]
  7824. and a
  7825. jr z, .setNumberOfHits
  7826. ld hl, wEnemyMoveEffect
  7827. .setNumberOfHits
  7828. ld a, [hl]
  7829. cp TWINEEDLE_EFFECT
  7830. jr z, .twineedle
  7831. cp ATTACK_TWICE_EFFECT
  7832. ld a, $2 ; number of hits it's always 2 for ATTACK_TWICE_EFFECT
  7833. jr z, .saveNumberOfHits
  7834. ; for TWO_TO_FIVE_ATTACKS_EFFECT 3/8 chance for 2 and 3 hits, and 1/8 chance for 4 and 5 hits
  7835. call BattleRandom
  7836. and $3
  7837. cp $2
  7838. jr c, .gotNumHits
  7839. ; if the number of hits was greater than 2, re-roll again for a lower chance
  7840. call BattleRandom
  7841. and $3
  7842. .gotNumHits
  7843. inc a
  7844. inc a
  7845. .saveNumberOfHits
  7846. ld [de], a
  7847. ld [bc], a
  7848. ret
  7849. .twineedle
  7850. ld a, POISON_SIDE_EFFECT1
  7851. ld [hl], a ; set Twineedle's effect to poison effect
  7852. jr .saveNumberOfHits
  7853. FlinchSideEffect:
  7854. call CheckTargetSubstitute
  7855. ret nz
  7856. ld hl, wEnemyBattleStatus1
  7857. ld de, wPlayerMoveEffect
  7858. ld a, [H_WHOSETURN]
  7859. and a
  7860. jr z, .flinchSideEffect
  7861. ld hl, wPlayerBattleStatus1
  7862. ld de, wEnemyMoveEffect
  7863. .flinchSideEffect
  7864. ld a, [de]
  7865. cp FLINCH_SIDE_EFFECT1
  7866. ld b, $1a ; ~10% chance of flinch
  7867. jr z, .gotEffectChance
  7868. ld b, $4d ; ~30% chance of flinch
  7869. .gotEffectChance
  7870. call BattleRandom
  7871. cp b
  7872. ret nc
  7873. set FLINCHED, [hl] ; set mon's status to flinching
  7874. call ClearHyperBeam
  7875. ret
  7876. OneHitKOEffect:
  7877. jpab OneHitKOEffect_
  7878. ChargeEffect:
  7879. ld hl, wPlayerBattleStatus1
  7880. ld de, wPlayerMoveEffect
  7881. ld a, [H_WHOSETURN]
  7882. and a
  7883. ld b, XSTATITEM_ANIM
  7884. jr z, .chargeEffect
  7885. ld hl, wEnemyBattleStatus1
  7886. ld de, wEnemyMoveEffect
  7887. ld b, ANIM_AF
  7888. .chargeEffect
  7889. set CHARGING_UP, [hl]
  7890. ld a, [de]
  7891. dec de ; de contains enemy or player MOVENUM
  7892. cp FLY_EFFECT
  7893. jr nz, .notFly
  7894. set INVULNERABLE, [hl] ; mon is now invulnerable to typical attacks (fly/dig)
  7895. ld b, TELEPORT ; load Teleport's animation
  7896. .notFly
  7897. ld a, [de]
  7898. cp DIG
  7899. jr nz, .notDigOrFly
  7900. set INVULNERABLE, [hl] ; mon is now invulnerable to typical attacks (fly/dig)
  7901. ld b, ANIM_C0
  7902. .notDigOrFly
  7903. xor a
  7904. ld [wAnimationType], a
  7905. ld a, b
  7906. call PlayBattleAnimation
  7907. ld a, [de]
  7908. ld [wChargeMoveNum], a
  7909. ld hl, ChargeMoveEffectText
  7910. jp PrintText
  7911. ChargeMoveEffectText:
  7912. TX_FAR _ChargeMoveEffectText
  7913. TX_ASM
  7914. ld a, [wChargeMoveNum]
  7915. cp RAZOR_WIND
  7916. ld hl, MadeWhirlwindText
  7917. jr z, .gotText
  7918. cp SOLARBEAM
  7919. ld hl, TookInSunlightText
  7920. jr z, .gotText
  7921. cp SKULL_BASH
  7922. ld hl, LoweredItsHeadText
  7923. jr z, .gotText
  7924. cp SKY_ATTACK
  7925. ld hl, SkyAttackGlowingText
  7926. jr z, .gotText
  7927. cp FLY
  7928. ld hl, FlewUpHighText
  7929. jr z, .gotText
  7930. cp DIG
  7931. ld hl, DugAHoleText
  7932. .gotText
  7933. ret
  7934. MadeWhirlwindText:
  7935. TX_FAR _MadeWhirlwindText
  7936. db "@"
  7937. TookInSunlightText:
  7938. TX_FAR _TookInSunlightText
  7939. db "@"
  7940. LoweredItsHeadText:
  7941. TX_FAR _LoweredItsHeadText
  7942. db "@"
  7943. SkyAttackGlowingText:
  7944. TX_FAR _SkyAttackGlowingText
  7945. db "@"
  7946. FlewUpHighText:
  7947. TX_FAR _FlewUpHighText
  7948. db "@"
  7949. DugAHoleText:
  7950. TX_FAR _DugAHoleText
  7951. db "@"
  7952. TrappingEffect:
  7953. ld hl, wPlayerBattleStatus1
  7954. ld de, wPlayerNumAttacksLeft
  7955. ld a, [H_WHOSETURN]
  7956. and a
  7957. jr z, .trappingEffect
  7958. ld hl, wEnemyBattleStatus1
  7959. ld de, wEnemyNumAttacksLeft
  7960. .trappingEffect
  7961. bit USING_TRAPPING_MOVE, [hl]
  7962. ret nz
  7963. call ClearHyperBeam ; since this effect is called before testing whether the move will hit,
  7964. ; the target won't need to recharge even if the trapping move missed
  7965. set USING_TRAPPING_MOVE, [hl] ; mon is now using a trapping move
  7966. call BattleRandom ; 3/8 chance for 2 and 3 attacks, and 1/8 chance for 4 and 5 attacks
  7967. and $3
  7968. cp $2
  7969. jr c, .setTrappingCounter
  7970. call BattleRandom
  7971. and $3
  7972. .setTrappingCounter
  7973. inc a
  7974. ld [de], a
  7975. ret
  7976. MistEffect:
  7977. jpab MistEffect_
  7978. FocusEnergyEffect:
  7979. jpab FocusEnergyEffect_
  7980. RecoilEffect:
  7981. jpab RecoilEffect_
  7982. ConfusionSideEffect:
  7983. call BattleRandom
  7984. cp $19 ; ~10% chance
  7985. ret nc
  7986. jr ConfusionSideEffectSuccess
  7987. ConfusionEffect:
  7988. call CheckTargetSubstitute
  7989. jr nz, ConfusionEffectFailed
  7990. call MoveHitTest
  7991. ld a, [wMoveMissed]
  7992. and a
  7993. jr nz, ConfusionEffectFailed
  7994. ConfusionSideEffectSuccess:
  7995. ld a, [H_WHOSETURN]
  7996. and a
  7997. ld hl, wEnemyBattleStatus1
  7998. ld bc, wEnemyConfusedCounter
  7999. ld a, [wPlayerMoveEffect]
  8000. jr z, .confuseTarget
  8001. ld hl, wPlayerBattleStatus1
  8002. ld bc, wPlayerConfusedCounter
  8003. ld a, [wEnemyMoveEffect]
  8004. .confuseTarget
  8005. bit CONFUSED, [hl] ; is mon confused?
  8006. jr nz, ConfusionEffectFailed
  8007. set CONFUSED, [hl] ; mon is now confused
  8008. push af
  8009. call BattleRandom
  8010. and $3
  8011. inc a
  8012. inc a
  8013. ld [bc], a ; confusion status will last 2-5 turns
  8014. pop af
  8015. cp CONFUSION_SIDE_EFFECT
  8016. call nz, PlayCurrentMoveAnimation2
  8017. ld hl, BecameConfusedText
  8018. jp PrintText
  8019. BecameConfusedText:
  8020. TX_FAR _BecameConfusedText
  8021. db "@"
  8022. ConfusionEffectFailed:
  8023. cp CONFUSION_SIDE_EFFECT
  8024. ret z
  8025. ld c, 50
  8026. call DelayFrames
  8027. jp ConditionalPrintButItFailed
  8028. ParalyzeEffect:
  8029. jpab ParalyzeEffect_
  8030. SubstituteEffect:
  8031. jpab SubstituteEffect_
  8032. HyperBeamEffect:
  8033. ld hl, wPlayerBattleStatus2
  8034. ld a, [H_WHOSETURN]
  8035. and a
  8036. jr z, .hyperBeamEffect
  8037. ld hl, wEnemyBattleStatus2
  8038. .hyperBeamEffect
  8039. set NEEDS_TO_RECHARGE, [hl] ; mon now needs to recharge
  8040. ret
  8041. ClearHyperBeam:
  8042. push hl
  8043. ld hl, wEnemyBattleStatus2
  8044. ld a, [H_WHOSETURN]
  8045. and a
  8046. jr z, .playerTurn
  8047. ld hl, wPlayerBattleStatus2
  8048. .playerTurn
  8049. res NEEDS_TO_RECHARGE, [hl] ; mon no longer needs to recharge
  8050. pop hl
  8051. ret
  8052. RageEffect:
  8053. ld hl, wPlayerBattleStatus2
  8054. ld a, [H_WHOSETURN]
  8055. and a
  8056. jr z, .player
  8057. ld hl, wEnemyBattleStatus2
  8058. .player
  8059. set USING_RAGE, [hl] ; mon is now in "rage" mode
  8060. ret
  8061. MimicEffect:
  8062. ld c, 50
  8063. call DelayFrames
  8064. call MoveHitTest
  8065. ld a, [wMoveMissed]
  8066. and a
  8067. jr nz, .mimicMissed
  8068. ld a, [H_WHOSETURN]
  8069. and a
  8070. ld hl, wBattleMonMoves
  8071. ld a, [wPlayerBattleStatus1]
  8072. jr nz, .enemyTurn
  8073. ld a, [wLinkState]
  8074. cp LINK_STATE_BATTLING
  8075. jr nz, .letPlayerChooseMove
  8076. ld hl, wEnemyMonMoves
  8077. ld a, [wEnemyBattleStatus1]
  8078. .enemyTurn
  8079. bit INVULNERABLE, a
  8080. jr nz, .mimicMissed
  8081. .getRandomMove
  8082. push hl
  8083. call BattleRandom
  8084. and $3
  8085. ld c, a
  8086. ld b, $0
  8087. add hl, bc
  8088. ld a, [hl]
  8089. pop hl
  8090. and a
  8091. jr z, .getRandomMove
  8092. ld d, a
  8093. ld a, [H_WHOSETURN]
  8094. and a
  8095. ld hl, wBattleMonMoves
  8096. ld a, [wPlayerMoveListIndex]
  8097. jr z, .playerTurn
  8098. ld hl, wEnemyMonMoves
  8099. ld a, [wEnemyMoveListIndex]
  8100. jr .playerTurn
  8101. .letPlayerChooseMove
  8102. ld a, [wEnemyBattleStatus1]
  8103. bit INVULNERABLE, a
  8104. jr nz, .mimicMissed
  8105. ld a, [wCurrentMenuItem]
  8106. push af
  8107. ld a, $1
  8108. ld [wMoveMenuType], a
  8109. call MoveSelectionMenu
  8110. call LoadScreenTilesFromBuffer1
  8111. ld hl, wEnemyMonMoves
  8112. ld a, [wCurrentMenuItem]
  8113. ld c, a
  8114. ld b, $0
  8115. add hl, bc
  8116. ld d, [hl]
  8117. pop af
  8118. ld hl, wBattleMonMoves
  8119. .playerTurn
  8120. ld c, a
  8121. ld b, $0
  8122. add hl, bc
  8123. ld a, d
  8124. ld [hl], a
  8125. ld [wd11e], a
  8126. call GetMoveName
  8127. call PlayCurrentMoveAnimation
  8128. ld hl, MimicLearnedMoveText
  8129. jp PrintText
  8130. .mimicMissed
  8131. jp PrintButItFailedText_
  8132. MimicLearnedMoveText:
  8133. TX_FAR _MimicLearnedMoveText
  8134. db "@"
  8135. LeechSeedEffect:
  8136. jpab LeechSeedEffect_
  8137. SplashEffect:
  8138. call PlayCurrentMoveAnimation
  8139. jp PrintNoEffectText
  8140. DisableEffect:
  8141. call MoveHitTest
  8142. ld a, [wMoveMissed]
  8143. and a
  8144. jr nz, .moveMissed
  8145. ld de, wEnemyDisabledMove
  8146. ld hl, wEnemyMonMoves
  8147. ld a, [H_WHOSETURN]
  8148. and a
  8149. jr z, .disableEffect
  8150. ld de, wPlayerDisabledMove
  8151. ld hl, wBattleMonMoves
  8152. .disableEffect
  8153. ; no effect if target already has a move disabled
  8154. ld a, [de]
  8155. and a
  8156. jr nz, .moveMissed
  8157. .pickMoveToDisable
  8158. push hl
  8159. call BattleRandom
  8160. and $3
  8161. ld c, a
  8162. ld b, $0
  8163. add hl, bc
  8164. ld a, [hl]
  8165. pop hl
  8166. and a
  8167. jr z, .pickMoveToDisable ; loop until a non-00 move slot is found
  8168. ld [wd11e], a ; store move number
  8169. push hl
  8170. ld a, [H_WHOSETURN]
  8171. and a
  8172. ld hl, wBattleMonPP
  8173. jr nz, .enemyTurn
  8174. ld a, [wLinkState]
  8175. cp LINK_STATE_BATTLING
  8176. pop hl ; wEnemyMonMoves
  8177. jr nz, .playerTurnNotLinkBattle
  8178. ; .playerTurnLinkBattle
  8179. push hl
  8180. ld hl, wEnemyMonPP
  8181. .enemyTurn
  8182. push hl
  8183. ld a, [hli]
  8184. or [hl]
  8185. inc hl
  8186. or [hl]
  8187. inc hl
  8188. or [hl]
  8189. and $3f
  8190. pop hl ; wBattleMonPP or wEnemyMonPP
  8191. jr z, .moveMissedPopHL ; nothing to do if all moves have no PP left
  8192. add hl, bc
  8193. ld a, [hl]
  8194. pop hl
  8195. and a
  8196. jr z, .pickMoveToDisable ; pick another move if this one had 0 PP
  8197. .playerTurnNotLinkBattle
  8198. ; non-link battle enemies have unlimited PP so the previous checks aren't needed
  8199. call BattleRandom
  8200. and $7
  8201. inc a ; 1-8 turns disabled
  8202. inc c ; move 1-4 will be disabled
  8203. swap c
  8204. add c ; map disabled move to high nibble of wEnemyDisabledMove / wPlayerDisabledMove
  8205. ld [de], a
  8206. call PlayCurrentMoveAnimation2
  8207. ld hl, wPlayerDisabledMoveNumber
  8208. ld a, [H_WHOSETURN]
  8209. and a
  8210. jr nz, .printDisableText
  8211. inc hl ; wEnemyDisabledMoveNumber
  8212. .printDisableText
  8213. ld a, [wd11e] ; move number
  8214. ld [hl], a
  8215. call GetMoveName
  8216. ld hl, MoveWasDisabledText
  8217. jp PrintText
  8218. .moveMissedPopHL
  8219. pop hl
  8220. .moveMissed
  8221. jp PrintButItFailedText_
  8222. MoveWasDisabledText:
  8223. TX_FAR _MoveWasDisabledText
  8224. db "@"
  8225. PayDayEffect:
  8226. jpab PayDayEffect_
  8227. ConversionEffect:
  8228. jpab ConversionEffect_
  8229. HazeEffect:
  8230. jpab HazeEffect_
  8231. HealEffect:
  8232. jpab HealEffect_
  8233. TransformEffect:
  8234. jpab TransformEffect_
  8235. ReflectLightScreenEffect:
  8236. jpab ReflectLightScreenEffect_
  8237. NothingHappenedText:
  8238. TX_FAR _NothingHappenedText
  8239. db "@"
  8240. PrintNoEffectText:
  8241. ld hl, NoEffectText
  8242. jp PrintText
  8243. NoEffectText:
  8244. TX_FAR _NoEffectText
  8245. db "@"
  8246. ConditionalPrintButItFailed:
  8247. ld a, [wMoveDidntMiss]
  8248. and a
  8249. ret nz ; return if the side effect failed, yet the attack was successful
  8250. PrintButItFailedText_:
  8251. ld hl, ButItFailedText
  8252. jp PrintText
  8253. ButItFailedText:
  8254. TX_FAR _ButItFailedText
  8255. db "@"
  8256. PrintDidntAffectText:
  8257. ld hl, DidntAffectText
  8258. jp PrintText
  8259. DidntAffectText:
  8260. TX_FAR _DidntAffectText
  8261. db "@"
  8262. IsUnaffectedText:
  8263. TX_FAR _IsUnaffectedText
  8264. db "@"
  8265. PrintMayNotAttackText:
  8266. ld hl, ParalyzedMayNotAttackText
  8267. jp PrintText
  8268. ParalyzedMayNotAttackText:
  8269. TX_FAR _ParalyzedMayNotAttackText
  8270. db "@"
  8271. CheckTargetSubstitute:
  8272. push hl
  8273. ld hl, wEnemyBattleStatus2
  8274. ld a, [H_WHOSETURN]
  8275. and a
  8276. jr z, .next1
  8277. ld hl, wPlayerBattleStatus2
  8278. .next1
  8279. bit HAS_SUBSTITUTE_UP, [hl]
  8280. pop hl
  8281. ret
  8282. PlayCurrentMoveAnimation2:
  8283. ; animation at MOVENUM will be played unless MOVENUM is 0
  8284. ; plays wAnimationType 3 or 6
  8285. ld a, [H_WHOSETURN]
  8286. and a
  8287. ld a, [wPlayerMoveNum]
  8288. jr z, .notEnemyTurn
  8289. ld a, [wEnemyMoveNum]
  8290. .notEnemyTurn
  8291. and a
  8292. ret z
  8293. PlayBattleAnimation2:
  8294. ; play animation ID at a and animation type 6 or 3
  8295. ld [wAnimationID], a
  8296. ld a, [H_WHOSETURN]
  8297. and a
  8298. ld a, $6
  8299. jr z, .storeAnimationType
  8300. ld a, $3
  8301. .storeAnimationType
  8302. ld [wAnimationType], a
  8303. jp PlayBattleAnimationGotID
  8304. PlayCurrentMoveAnimation:
  8305. ; animation at MOVENUM will be played unless MOVENUM is 0
  8306. ; resets wAnimationType
  8307. xor a
  8308. ld [wAnimationType], a
  8309. ld a, [H_WHOSETURN]
  8310. and a
  8311. ld a, [wPlayerMoveNum]
  8312. jr z, .notEnemyTurn
  8313. ld a, [wEnemyMoveNum]
  8314. .notEnemyTurn
  8315. and a
  8316. ret z
  8317. PlayBattleAnimation:
  8318. ; play animation ID at a and predefined animation type
  8319. ld [wAnimationID], a
  8320. PlayBattleAnimationGotID:
  8321. ; play animation at wAnimationID
  8322. push hl
  8323. push de
  8324. push bc
  8325. predef MoveAnimation
  8326. pop bc
  8327. pop de
  8328. pop hl
  8329. ret