OSPATHS2.cpp 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OSPATHS2.CPP
  21. //Description : Object SeekPathS2
  22. //Owner : Alex
  23. #include <math.h>
  24. #include <stdlib.h>
  25. #include <ALL.h>
  26. #include <OWORLD.h>
  27. #include <OSPATHS2.h>
  28. #include <OUNIT.h>
  29. #include <OFIRM.h>
  30. #include <OTOWN.h>
  31. #include <OSYS.h>
  32. //----------- Define static variables -----------//
  33. static Location* world_loc_matrix;
  34. static int cur_stack_pos_s2 = 0;
  35. static NodeS2* stack_array[MAX_STACK_NUM];
  36. static DWORD group_id_s2;
  37. static short search_mode_s2;
  38. static char mobile_type_s2;
  39. static short target_recno_s2;
  40. static short building_id_s2; // used in search_mode = SEARCH_MODE_TO_FIRM or SEARCH_MODE_TO_TOWN, get from miscNo
  41. static int building_x1_s2, building_y1_s2, building_x2_s2, building_y2_s2;
  42. // the building upper-left to lower-right positions
  43. // if search_mode = SEARCH_MODE_TO_FIRM or SEARCH_MODE_TO_TOWN
  44. static FirmInfo *searchFirmInfo_s2;
  45. static short x_shift_array[8] = { 1, 1, 0, -1, -1, -1, 0, 1};
  46. static short y_shift_array[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
  47. //---- used in determining the return nodes in the starting node -----//
  48. static char source_blocked_exit_direction[8]; // indicate which exit direction of starting node is impossible
  49. static char source_locate_type; // indicate the upper left position of the unit located in the 2x2 node
  50. //---- used to decide which path is return in a node -------//
  51. static short prefer_upper_s2; // if upper and lower paths are available, true if the upper path is preferred
  52. static short prefer_lower_s2; // if upper and lower paths are available, true if the lower path is preferred
  53. static short prefer_left_s2; // if left and right paths are available, true if the left path is preferred
  54. static short prefer_right_s2; // if left and right paths are available, true if the right path is preferred
  55. //--- show whether shift-map method is used the direction shifted ---//
  56. static short use_shift_map_method_s2;// is 0 or 1, 1 for using this method
  57. static short shift_map_x_offset_s2; // useful only as shift-map-method is applied
  58. static short shift_map_y_offset_s2; // useful only as shift-map-method is applied
  59. //static short reverse_direction_result_node;
  60. //-- store the orginal and the finalized starting and destination location --//
  61. static short source_x_s2; // starting x location in 2x2 node format
  62. static short source_y_s2; // starting y location in 2x2 node format
  63. static int vir_start_x_s2; // store the start x loc after applying shift-map-method
  64. static int vir_start_y_s2; // store the start y loc after applying shift-map-method
  65. static int final_dest_x_s2; // store the finalize x loc after applying shift-map-method, path-reuse, and etc
  66. static int final_dest_y_s2; // store the finalize y loc after applying shift-map-method, path-reuse, and etc
  67. //------------------- used in path-reuse -----------------------//
  68. static int max_node_num_s2;
  69. static short *reuse_node_matrix_ptr_s2;
  70. static NodeS2 *reuse_result_node_ptr_s2;
  71. static int final_dx;
  72. static int final_dy;
  73. //------- aliasing class member vars for fast access ------//
  74. static SeekPathS2* cur_seek_path_s2;
  75. static short cur_dest_x_s2, cur_dest_y_s2;
  76. static short* cur_node_matrix_s2;
  77. static NodeS2* cur_node_array_s2;
  78. static short cur_border_x1_s2, cur_border_y1_s2, cur_border_x2_s2, cur_border_y2_s2;
  79. #ifdef DEBUG
  80. static ResultNode *debugPtr1, *debugPtr2;
  81. static short debugVX, debugVY;
  82. static int debugCount;
  83. void debug_checks2(ResultNode *nodeArray, int count)
  84. {
  85. debugPtr1 = nodeArray;
  86. debugPtr2 = nodeArray+1;
  87. for(debugCount=1; debugCount<count; debugCount++, debugPtr1++, debugPtr2++)
  88. {
  89. err_when(debugPtr1->node_x<0 || debugPtr1->node_x>=MAX_WORLD_X_LOC-1 ||
  90. debugPtr1->node_y<0 || debugPtr1->node_y>=MAX_WORLD_Y_LOC-1);
  91. debugVX = debugPtr1->node_x - debugPtr2->node_x;
  92. debugVY = debugPtr1->node_y - debugPtr2->node_y;
  93. err_when(debugVX!=0 && debugVY!=0 && (abs(debugVX)!=abs(debugVY)));
  94. }
  95. err_when(debugPtr1->node_x<0 || debugPtr1->node_x>=MAX_WORLD_X_LOC-1 ||
  96. debugPtr1->node_y<0 || debugPtr1->node_y>=MAX_WORLD_Y_LOC-1);
  97. }
  98. #endif
  99. #ifdef DEBUG
  100. #define debug_check_paths2(nodeArray, count) debug_checks2((nodeArray), (count))
  101. #else
  102. #define debug_check_paths2(nodeArray, count)
  103. #endif
  104. //----------- Define static functions -----------//
  105. static void stack_push(NodeS2 *nodePtr);
  106. static NodeS2* stack_pop();
  107. //------- Begin of static function sys_yield ----------//
  108. static void sys_yield()
  109. {
  110. //sys.yield();
  111. }
  112. //------- End of static function sys_yield ----------//
  113. //------- Begin of static function can_move_to ----------//
  114. static int can_move_to(int xLoc, int yLoc) // xLoc, yLoc is location after applying shift-map-method
  115. {
  116. //-------------------------------------------------------//
  117. // note : return value should be 0 (false) or 1 (true)
  118. //-------------------------------------------------------//
  119. int x, y;
  120. //-------------------- boundary checking -----------------//
  121. if(shift_map_x_offset_s2==0)
  122. {
  123. if(xLoc<0 || xLoc>=MAX_WORLD_X_LOC)
  124. return 0;
  125. else
  126. x = xLoc;
  127. }
  128. else // shifted
  129. {
  130. if(xLoc<1 || xLoc>MAX_WORLD_X_LOC)
  131. return 0;
  132. else
  133. x = xLoc+shift_map_x_offset_s2;
  134. }
  135. if(shift_map_y_offset_s2==0)
  136. {
  137. if(yLoc<0 || yLoc>=MAX_WORLD_Y_LOC)
  138. return 0;
  139. else
  140. y = yLoc;
  141. }
  142. else // shifted
  143. {
  144. if(yLoc<1 || yLoc>MAX_WORLD_Y_LOC)
  145. return 0;
  146. else
  147. y = yLoc+shift_map_y_offset_s2;
  148. }
  149. Location* locPtr = world_loc_matrix+y*MAX_WORLD_X_LOC+x;
  150. Unit* unitPtr;
  151. //---------- checking whether the location is walkable ----------//
  152. switch(mobile_type_s2)
  153. {
  154. case UNIT_LAND:
  155. if(!locPtr->walkable() && search_mode_s2<SEARCH_MODE_TO_FIRM) // be careful for the search_mode>=SEARCH_MODE_TO_FIRM
  156. return 0;
  157. switch(search_mode_s2)
  158. {
  159. case SEARCH_MODE_IN_A_GROUP: // group move
  160. case SEARCH_MODE_REUSE: // path-reuse
  161. if(!locPtr->cargo_recno)
  162. return 1;
  163. else
  164. {
  165. unitPtr = unit_array[locPtr->cargo_recno];
  166. return (unitPtr->unit_group_id==group_id_s2 && unitPtr->cur_action!=SPRITE_ATTACK) ||
  167. unitPtr->cur_action==SPRITE_MOVE;
  168. }
  169. case SEARCH_MODE_A_UNIT_IN_GROUP: // a unit in a group
  170. return locPtr->cargo_recno==0 || unit_array[locPtr->cargo_recno]->cur_action==SPRITE_MOVE;
  171. case SEARCH_MODE_TO_ATTACK: // to attack target
  172. if(!locPtr->cargo_recno)
  173. return 1;
  174. else
  175. {
  176. unitPtr = unit_array[locPtr->cargo_recno];
  177. return (unitPtr->unit_group_id==group_id_s2 && unitPtr->cur_action!=SPRITE_ATTACK) ||
  178. locPtr->cargo_recno==target_recno_s2 || unitPtr->cur_action==SPRITE_MOVE;
  179. }
  180. case SEARCH_MODE_BLOCKING: // 2x2 unit blocking
  181. if(locPtr->cargo_recno)
  182. unitPtr = unit_array[locPtr->cargo_recno];
  183. return locPtr->cargo_recno==0 ||
  184. (unitPtr->unit_group_id==group_id_s2 && (unitPtr->cur_action==SPRITE_MOVE || unitPtr->cur_action==SPRITE_READY_TO_MOVE));
  185. case SEARCH_MODE_TO_VEHICLE:
  186. err_here(); //** error
  187. return 0;
  188. //-----------------------------------------------------------------------//
  189. // for the following search_mode, location may be treated as walkable
  190. // although it is not.
  191. //-----------------------------------------------------------------------//
  192. case SEARCH_MODE_TO_FIRM: // move to a firm, (location may be not walkable)
  193. case SEARCH_MODE_TO_TOWN: // move to a town zone, (location may be not walkable)
  194. return (locPtr->walkable() && (locPtr->cargo_recno==0 ||
  195. (unit_array[locPtr->cargo_recno]->unit_group_id==group_id_s2 &&
  196. unit_array[locPtr->cargo_recno]->cur_action!=SPRITE_ATTACK) )) ||
  197. (xLoc>=building_x1_s2 && xLoc<=building_x2_s2 && yLoc>=building_y1_s2 && yLoc<=building_y2_s2);
  198. case SEARCH_MODE_TO_WALL_FOR_GROUP: // move to wall for a group, (location may be not walkable)
  199. return (locPtr->walkable() && (locPtr->cargo_recno==0 ||
  200. (unit_array[locPtr->cargo_recno]->unit_group_id==group_id_s2 &&
  201. unit_array[locPtr->cargo_recno]->cur_action!=SPRITE_ATTACK) )) ||
  202. (xLoc==final_dest_x_s2 && yLoc==final_dest_y_s2);
  203. case SEARCH_MODE_TO_WALL_FOR_UNIT: // move to wall for a unit only, (location may be not walkable)
  204. return (locPtr->walkable() && locPtr->cargo_recno==0) ||
  205. (xLoc==final_dest_x_s2 && yLoc==final_dest_y_s2);
  206. }
  207. break;
  208. case UNIT_SEA:
  209. if( !locPtr->sailable() )
  210. return 0;
  211. if( search_mode_s2== SEARCH_MODE_A_UNIT_IN_GROUP)
  212. return locPtr->cargo_recno==0;
  213. else
  214. return locPtr->cargo_recno==0 || unit_array[locPtr->cargo_recno]->unit_group_id==group_id_s2 ||
  215. (search_mode_s2==SEARCH_MODE_TO_ATTACK && locPtr->cargo_recno==target_recno_s2);
  216. break;
  217. case UNIT_AIR:
  218. if( search_mode_s2== SEARCH_MODE_A_UNIT_IN_GROUP)
  219. return locPtr->air_cargo_recno==0;
  220. else
  221. return locPtr->air_cargo_recno==0 || unit_array[locPtr->air_cargo_recno]->unit_group_id==group_id_s2 ||
  222. (search_mode_s2==SEARCH_MODE_TO_ATTACK && locPtr->air_cargo_recno==target_recno_s2);
  223. break;
  224. default:
  225. err_here();
  226. break;
  227. }
  228. return 0;
  229. }
  230. //------- End of static function can_move_to ----------//
  231. //------- Begin of static function can_move_to_s2 ----------//
  232. static int can_move_to_s2(int xLoc, int yLoc) // xLoc, yLoc is location after applying shift-map-method
  233. {
  234. //------------------------------------------------//
  235. // note: return value should be 0 or 1
  236. //------------------------------------------------//
  237. if(can_move_to(xLoc,yLoc) && can_move_to(xLoc+1,yLoc) &&
  238. can_move_to(xLoc,yLoc+1) && can_move_to(xLoc+1,yLoc+1))
  239. return 1; // is walkable
  240. else
  241. return 0; // is not walkable as one point is not walkable
  242. }
  243. //------- End of static function can_move_to_s2 ----------//
  244. //-------- Begin of function SeekPathS2::init ---------//
  245. void SeekPathS2::init(int maxNode)
  246. {
  247. max_node = maxNode;
  248. node_array = (NodeS2*) mem_add( max_node * sizeof(NodeS2) );
  249. node_matrix = (short*) mem_add(sizeof(short)*(MAX_WORLD_X_LOC/2+1)*(MAX_WORLD_Y_LOC/2+1));
  250. path_status = PATH_WAIT;
  251. open_node_list = NULL;
  252. closed_node_list = NULL;
  253. }
  254. //--------- End of function SeekPathS2::init ---------//
  255. //-------- Begin of function SeekPathS2::deinit ---------//
  256. void SeekPathS2::deinit()
  257. {
  258. if( node_array )
  259. {
  260. mem_del(node_array);
  261. node_array = NULL;
  262. }
  263. if( node_matrix )
  264. {
  265. mem_del(node_matrix);
  266. node_matrix = NULL;
  267. }
  268. }
  269. //--------- End of function SeekPathS2::deinit ---------//
  270. //-------- Begin of function SeekPathS2::set_node_matrix ---------//
  271. void SeekPathS2::set_node_matrix(short reuseNodeMatrix[])
  272. {
  273. reuse_node_matrix_ptr_s2 = reuseNodeMatrix;
  274. }
  275. //--------- End of function SeekPathS2::set_node_matrix ---------//
  276. //-------- Begin of function SeekPathS2::reset ---------//
  277. void SeekPathS2::reset()
  278. {
  279. path_status=PATH_WAIT;
  280. open_node_list=NULL;
  281. closed_node_list=NULL;
  282. }
  283. //--------- End of function SeekPathS2::reset ---------//
  284. //-------- Begin of function SeekPathS2::add_result_node ---------//
  285. inline void SeekPathS2::add_result_node(ResultNode** curPtr, ResultNode** prePtr, int& count)
  286. {
  287. err_when(abs((*curPtr)->node_x-(*prePtr)->node_x)>1 || abs((*curPtr)->node_y-(*prePtr)->node_y)>1);
  288. *prePtr = *curPtr;
  289. (*curPtr)++;
  290. count++;
  291. }
  292. //--------- End of function SeekPathS2::add_result_node ---------//
  293. //-------- Begin of function SeekPathS2::seek ---------//
  294. //
  295. // <int> sx, sy - the starting world location.
  296. // <int> dx, dy - the destination world location.
  297. // <DWORD> groupId - unit group id.
  298. // <char> mobileType - mobile type, can be UNIT_AIR, UNIT_LAND or UNIT_SEA
  299. //
  300. // [int] searchMode - 1, SEARCH_MODE_IN_A_GROUP for one group with an unique group id (default)
  301. // 2, SEARCH_MODE_A_UNIT_IN_GROUP for one sprite in a group
  302. // 3, SEARCH_MODE_TO_ATTACK for attacking
  303. // 4, SEARCH_MODE_REUSE for path reuse
  304. // 5, SEARCH_MODE_BLOCKING for 2x2 unit blocked searching
  305. // 6, SEARCH_MODE_TO_FIRM for moving to a firm
  306. // 7, SEARCH_MODE_TO_TOWN for moving to a town zone
  307. // 8, SEARCH_MODE_TO_VEHICLE for moving to a vehicle
  308. // (default : 1)
  309. //
  310. // [int] miscNo - target record no if searchMode=SEARCH_MODE_TO_ATTACK
  311. // - firm ID if searchMode=SEARCH_MODE_TO_FIRM
  312. //
  313. // [int] maxTries - maximum no. of tries in the first seek action.
  314. // this refer to the maximum no. of nodes created.
  315. // (default : max_node)
  316. //
  317. // [int] borderX1, borderY1, - borders of the seek area in the world map
  318. // borderX2, borderY2 (default: the whole map)
  319. //
  320. // Note: if maxTries==max_node, incremental seek (PATH_SEEKING) won't happen.
  321. //
  322. // return : <int> seekStatus - PATH_FOUND, PATH_SEEKING, PATH_NODE_USED_UP, or PATH_IMPOSSIBLE
  323. // if PATH_FOUND, or PATH_NODE_USED_UP, can call get_result() to retrieve the result.
  324. //
  325. int SeekPathS2::seek(int sx,int sy,int dx,int dy,DWORD groupId,char mobileType,short searchMode,short miscNo,
  326. int maxTries,int borderX1,int borderY1,int borderX2,int borderY2)
  327. {
  328. //-------- initialize vars --------------//
  329. path_status = PATH_SEEKING;
  330. world_loc_matrix = world.loc_matrix; // alias for fast access
  331. open_node_list = NULL;
  332. closed_node_list = NULL;
  333. search_mode_s2 = searchMode;
  334. group_id_s2 = groupId;
  335. mobile_type_s2 = mobileType;
  336. //------------------------------------------------------------------------------//
  337. // extract informaton from the parameter "miscNo"
  338. //------------------------------------------------------------------------------//
  339. target_recno_s2 = building_id_s2 = 0;
  340. building_x1_s2 = building_y1_s2 = building_x2_s2 = building_y2_s2 = -1;
  341. err_when(search_mode_s2==SEARCH_MODE_TO_VEHICLE);
  342. if(search_mode_s2==SEARCH_MODE_TO_ATTACK)
  343. target_recno_s2 = miscNo;
  344. else
  345. {
  346. building_id_s2 = miscNo;
  347. building_x1_s2 = dx; // upper left corner location
  348. building_y1_s2 = dy;
  349. //------- calculate the lower right corner location -------//
  350. if(search_mode_s2==SEARCH_MODE_TO_FIRM)
  351. {
  352. searchFirmInfo_s2 = firm_res[building_id_s2];
  353. building_x2_s2 = dx+searchFirmInfo_s2->loc_width-1;
  354. building_y2_s2 = dy+searchFirmInfo_s2->loc_height-1;
  355. }
  356. else if(search_mode_s2==SEARCH_MODE_TO_TOWN)
  357. {
  358. if(miscNo != -1)
  359. {
  360. Location* buildPtr = world.get_loc(dx, dy);
  361. Town* targetTown = town_array[buildPtr->town_recno()];
  362. building_x2_s2 = targetTown->loc_x2;
  363. building_y2_s2 = targetTown->loc_y2;
  364. }
  365. else // searching to settle. Detail explained in function set_move_to_surround()
  366. {
  367. building_x2_s2 = building_x1_s2+STD_TOWN_LOC_WIDTH-1;
  368. building_y2_s2 = building_y1_s2+STD_TOWN_LOC_HEIGHT-1;
  369. }
  370. }
  371. }
  372. //------------------------------------------------------------------------------//
  373. // set start location and destination location
  374. //------------------------------------------------------------------------------//
  375. real_sour_x = sx;
  376. real_sour_y = sy;
  377. //---------- adjust destination for some kind of searching ------------//
  378. if(search_mode_s2==SEARCH_MODE_TO_FIRM || search_mode_s2==SEARCH_MODE_TO_TOWN)
  379. {
  380. final_dx = (building_x1_s2+building_x2_s2)/2; // the destination is set to be the middle of the building
  381. final_dy = (building_y1_s2+building_y2_s2)/2;
  382. }
  383. else
  384. {
  385. //---------- no adjustment --------//
  386. final_dx = real_dest_x = dx;
  387. final_dy = real_dest_y = dy;
  388. }
  389. //------------------------------------------------------------------------------//
  390. // check whether applying shift-map-method
  391. //------------------------------------------------------------------------------//
  392. if(final_dx%2) // perform x-shift
  393. {
  394. use_shift_map_method_s2 = 1;
  395. shift_map_x_offset_s2 = -1;
  396. vir_start_x_s2 = sx+1;
  397. final_dest_x_s2 = final_dx+1;
  398. }
  399. else
  400. {
  401. use_shift_map_method_s2 = 0;
  402. shift_map_x_offset_s2 = 0;
  403. vir_start_x_s2 = sx;
  404. final_dest_x_s2 = final_dx;
  405. }
  406. if(final_dy%2) // perform y-shift
  407. {
  408. use_shift_map_method_s2 = 1;
  409. shift_map_y_offset_s2 = -1;
  410. vir_start_y_s2 = sy+1;
  411. final_dest_y_s2 = final_dy+1;
  412. }
  413. else
  414. {
  415. //use_shift_map_method_s2 = 0; // have been set to zero in the previous check
  416. shift_map_y_offset_s2 = 0;
  417. vir_start_y_s2 = sy;
  418. final_dest_y_s2 = final_dy;
  419. }
  420. //--------------------------------------------------------------//
  421. // change to 2x2 node format
  422. //--------------------------------------------------------------//
  423. border_x1 = short(borderX1/2); // change to 2x2 node format
  424. border_y1 = short(borderY1/2);
  425. border_x2 = short(borderX2/2);
  426. border_y2 = short(borderY2/2);
  427. if(shift_map_x_offset_s2!=0) // adjust if shift-map is used
  428. border_x2++;
  429. if(shift_map_y_offset_s2!=0)
  430. border_y2++;
  431. //-------------- initialize the array -----------------//
  432. memset(source_blocked_exit_direction, 0, sizeof(char)*8);
  433. //----- determine the blocked exit direction in starting point----------//
  434. // there are four cases the real source located in the 2x2 node
  435. //
  436. // -- -- the number in the 2x2 node representing where the
  437. // | 1| 2| upper_left location of the unit is
  438. // -- --
  439. // | 3| 4|
  440. // -- --
  441. if(vir_start_y_s2%2 != 1)
  442. {
  443. if(vir_start_x_s2%2 != 1) // case 1
  444. source_locate_type = 1;
  445. else // case 2
  446. {
  447. source_locate_type = 2;
  448. if(!can_move_to(vir_start_x_s2-1, vir_start_y_s2+1) && !can_move_to(vir_start_x_s2, vir_start_y_s2-1))
  449. source_blocked_exit_direction[7] = 1; // direction 8
  450. if(!can_move_to(vir_start_x_s2-1, vir_start_y_s2) && !can_move_to(vir_start_x_s2, vir_start_y_s2+2))
  451. source_blocked_exit_direction[1] = 1; // direction 2
  452. // other cases will not be checked as these cases do not crack the search
  453. }
  454. }
  455. else
  456. {
  457. if(vir_start_x_s2%2 != 1) // case 3
  458. {
  459. source_locate_type = 3;
  460. if(!can_move_to(vir_start_x_s2, vir_start_y_s2-1) && !can_move_to(vir_start_x_s2+2, vir_start_y_s2))
  461. source_blocked_exit_direction[5] = 1; // direction 6
  462. if(!can_move_to(vir_start_x_s2-1, vir_start_y_s2) && !can_move_to(vir_start_x_s2+1, vir_start_y_s2-1))
  463. source_blocked_exit_direction[7] = 1; // direction 8
  464. // other cases will not be checked as these cases do not crack the search
  465. }
  466. else // case 4
  467. {
  468. source_locate_type = 4;
  469. if(!can_move_to(vir_start_x_s2, vir_start_y_s2-1) || !can_move_to(vir_start_x_s2-1, vir_start_y_s2))
  470. source_blocked_exit_direction[7] = 1; // direction 8
  471. if(!can_move_to(vir_start_x_s2-1, vir_start_y_s2+1) && !can_move_to(vir_start_x_s2, vir_start_y_s2-1))
  472. source_blocked_exit_direction[0] = 1; // direction 1
  473. if(!can_move_to(vir_start_x_s2-1, vir_start_y_s2) && !can_move_to(vir_start_x_s2+1, vir_start_y_s2-1))
  474. source_blocked_exit_direction[6] = 1; // direction 7
  475. // other cases will not be checked as these cases do not crack the search
  476. }
  477. }
  478. source_x_s2 = short(vir_start_x_s2/2); // the upper left corner is used
  479. source_y_s2 = short(vir_start_y_s2/2);
  480. err_when(final_dest_x_s2%2 || final_dest_y_s2%2);
  481. dest_x = short(final_dest_x_s2/2);
  482. dest_y = short(final_dest_y_s2/2);
  483. if(search_mode_s2!=SEARCH_MODE_REUSE)
  484. {
  485. max_node_num_s2 = 0xFFFF;
  486. memset(node_matrix, 0, sizeof(short)*((MAX_WORLD_X_LOC/2+1)*(MAX_WORLD_Y_LOC/2+1)));
  487. }
  488. else
  489. {
  490. max_node_num_s2 = max_node;
  491. memcpy(node_matrix, reuse_node_matrix_ptr_s2, sizeof(short)*((MAX_WORLD_X_LOC/2+1)*(MAX_WORLD_Y_LOC/2+1)));
  492. }
  493. //--------- create the first node --------//
  494. node_count = 0;
  495. result_node_ptr = NULL;
  496. NodeS2 *nodePtr = node_array + node_count++;
  497. memset(nodePtr, 0, sizeof(NodeS2));
  498. //-------- store the upper left coordinate of the node ----------//
  499. upper_left_x = source_x_s2<<1;
  500. upper_left_y = source_y_s2<<1;
  501. //---------- calculate the node type -----------//
  502. switch(source_locate_type)
  503. {
  504. case 1: nodePtr->node_type = 15;
  505. break;
  506. case 2: nodePtr->node_type = 10+can_move_to(vir_start_x_s2-1,vir_start_y_s2)+
  507. can_move_to(vir_start_x_s2-1,vir_start_y_s2+1)*4;
  508. break;
  509. case 3: nodePtr->node_type = 12+can_move_to(vir_start_x_s2,vir_start_y_s2-1)+
  510. can_move_to(vir_start_x_s2+1,vir_start_y_s2-1)*2;
  511. break;
  512. case 4: nodePtr->node_type = 8+can_move_to(vir_start_x_s2-1,vir_start_y_s2-1)+
  513. can_move_to(vir_start_x_s2,vir_start_y_s2-1)*2+
  514. can_move_to(vir_start_x_s2-1,vir_start_y_s2)*4;
  515. break;
  516. default: err_here();
  517. break;
  518. }
  519. err_when(nodePtr->node_type>15 || nodePtr->node_type <0);
  520. is_dest_blocked = !can_move_to_s2(final_dest_x_s2, final_dest_y_s2);
  521. // whether the destination is blocked, if so, only search till the destination's neighbor locations
  522. nodePtr->node_g = 0;
  523. nodePtr->node_h = (source_x_s2-dest_x)*(source_x_s2-dest_x)+(source_y_s2-dest_y)*(source_y_s2-dest_y); // should really use sqrt().
  524. nodePtr->node_f = nodePtr->node_g + nodePtr->node_h;
  525. nodePtr->node_x = source_x_s2;
  526. nodePtr->node_y = source_y_s2;
  527. nodePtr->enter_direction = 0;
  528. open_node_list=nodePtr; // make Open List point to first node
  529. //--- if the destination is the current postion or next to it & the dest is occupied ---//
  530. if(source_x_s2==dest_x && source_y_s2==dest_y)
  531. {
  532. path_status = PATH_FOUND;
  533. result_node_ptr = nodePtr;
  534. return path_status;
  535. }
  536. //------------ seek now ------------------//
  537. if( !maxTries )
  538. maxTries = max_node;
  539. return continue_seek( maxTries, 1); // 1-first seek session of the current seek order
  540. }
  541. //-------- End of function SeekPathS2::seek ---------//
  542. //---- Begin of function SeekPathS2::continue_seek ---------//
  543. //
  544. // If the last seek operation does not find the whole path,
  545. // continue the search.
  546. //
  547. // <int> maxTries - maximum path seeking tries
  548. // [char] firstSeek - whether it's the first seek session of the seek order.
  549. // (default: 0)
  550. //
  551. // return : <int> seekStatus - PATH_FOUND, PATH_SEEKING, PATH_NODE_USED_UP,
  552. // or PATH_IMPOSSIBLE
  553. //
  554. // You can call get_result() to retrieve the result.
  555. //
  556. int SeekPathS2::continue_seek(int maxTries, char firstSeek)
  557. {
  558. if( path_status != PATH_SEEKING )
  559. return path_status;
  560. //------- aliasing class member vars for fast access ------//
  561. cur_seek_path_s2 = this;
  562. cur_dest_x_s2 = dest_x;
  563. cur_dest_y_s2 = dest_y;
  564. cur_node_matrix_s2 = node_matrix;
  565. cur_node_array_s2 = node_array;
  566. cur_border_x1_s2 = border_x1;
  567. cur_border_y1_s2 = border_y1;
  568. cur_border_x2_s2 = border_x2;
  569. cur_border_y2_s2 = border_y2;
  570. //------ seek the path using the A star algorithm -----//
  571. NodeS2 *bestNodePtr;
  572. int maxNode = max_node-MAX_CHILD_NODE; // generate_successors() can generate a max of MAX_CHILD_NODE new nodes per call
  573. if(maxTries < maxNode)
  574. maxNode = maxTries-MAX_CHILD_NODE; // limit the nodes used in shortest path searching
  575. for(int i=0; i<maxTries ; i++ )
  576. {
  577. bestNodePtr = return_best_node();
  578. if(i%20==0)
  579. sys_yield(); // update cursor position
  580. //---- even if the path is impossible, get the closest path ----//
  581. if( !bestNodePtr )
  582. {
  583. path_status = PATH_IMPOSSIBLE;
  584. break;
  585. }
  586. //----- exceed the object's max's node limitation, return the closest path ----//
  587. if( node_count >= maxNode )
  588. {
  589. path_status = PATH_NODE_USED_UP;
  590. break;
  591. }
  592. //------------------------------------------//
  593. // If the path is found OR
  594. //
  595. // If the destination is blocked,
  596. // consider it done when we are next to it.
  597. //------------------------------------------//
  598. if((bestNodePtr->node_x==dest_x && bestNodePtr->node_y==dest_y) ||
  599. (is_dest_blocked && abs(bestNodePtr->node_x-dest_x)<=0 && abs(bestNodePtr->node_y-dest_y)<=0))
  600. {
  601. path_status = PATH_FOUND;
  602. result_node_ptr = bestNodePtr;
  603. break;
  604. }
  605. //--------- generate successors -------//
  606. if(bestNodePtr->generate_successors(bestNodePtr->enter_direction))
  607. {
  608. path_status = PATH_REUSE_FOUND;
  609. result_node_ptr = reuse_result_node_ptr_s2;
  610. return path_status;
  611. }
  612. }
  613. err_when( cur_stack_pos_s2!=0 ); // it should be zero all the times, all pushes should have been poped
  614. return path_status;
  615. }
  616. //------ End of function SeekPathS2::continue_seek ---------//
  617. //---- Begin of function SeekPathS2::get_result ---------//
  618. //
  619. // Compile the seek result nodes using results processed by
  620. // seek() and continue_seek() and store the results in
  621. // an array of ResultNode.
  622. //
  623. // <int&> resultNodeCount - a reference var for returning the no. of result nodes.
  624. //
  625. // return : <ResultNode*> an array of ResultNode.
  626. // the caller function is responsible for
  627. // freeing the memory of the array.
  628. //
  629. ResultNode* SeekPathS2::get_result(int& resultNodeCount, short& pathDist)
  630. {
  631. resultNodeCount = pathDist = 0;
  632. short useClosestNode = 0;// indicate whether closest node is return instead of the actual node
  633. if( !result_node_ptr ) // if PATH_IMPOSSIBLE or PATH_NODE_USED_UP, result_node_ptr is NULL, we need to call get_closest_node() to get the closest node.
  634. {
  635. result_node_ptr = return_closest_node();
  636. useClosestNode = 1;
  637. if( !result_node_ptr )
  638. return NULL;
  639. }
  640. //--------------------------------------------------//
  641. // Trace backwards to the starting node, and rationalize
  642. // nodes (i.e. group nodes of the same direction into
  643. // single nodes.)
  644. //--------------------------------------------------//
  645. NodeS2* nodePtr = result_node_ptr; // the node current being processed
  646. NodeS2* baseNodePtr = result_node_ptr; // the first end node for connecting the other end node for the path in that direction.
  647. NodeS2* parentNode = nodePtr->parent_node; // the parent node of nodePtr
  648. NodeS2* childNodePtr = nodePtr; // it should point to the children node of nodePtr
  649. //------------------------------------------------------------------------
  650. // there are only one node, source & destination within the same 2x2 node
  651. //------------------------------------------------------------------------
  652. if( !parentNode ) // parentNode==0 when the source location is the desination already
  653. {
  654. if( (vir_start_x_s2!=final_dest_x_s2 || vir_start_y_s2!=final_dest_y_s2) &&
  655. abs(vir_start_x_s2-final_dest_x_s2)<=1 && abs(vir_start_y_s2-final_dest_y_s2)<=1 &&
  656. can_move_to_s2(vir_start_x_s2,vir_start_y_s2) && // for source
  657. can_move_to_s2(final_dest_x_s2,final_dest_y_s2)) // for destination
  658. {
  659. pathDist = 1;
  660. ResultNode* resultNodeArray1 = (ResultNode*) mem_add(sizeof(ResultNode)*2);
  661. ResultNode* resultNodePtr1 = resultNodeArray1;
  662. resultNodeCount=2;
  663. resultNodePtr1->node_x = vir_start_x_s2 + shift_map_x_offset_s2;
  664. resultNodePtr1->node_y = vir_start_y_s2 + shift_map_y_offset_s2;
  665. resultNodePtr1++;
  666. resultNodePtr1->node_x = final_dest_x_s2 + shift_map_x_offset_s2;
  667. resultNodePtr1->node_y = final_dest_y_s2 + shift_map_y_offset_s2;
  668. #ifdef DEBUG
  669. debugPtr1 = resultNodeArray1;
  670. debugPtr2 = resultNodeArray1+1;
  671. err_when(debugPtr1->node_x==debugPtr2->node_x && debugPtr1->node_y==debugPtr2->node_y);
  672. #endif
  673. return resultNodeArray1;
  674. }
  675. else
  676. return NULL;
  677. }
  678. resultNodeCount = 1;
  679. //-----------------------------------
  680. // count the number of 2x2 node
  681. //-----------------------------------
  682. int numOfNode=0;
  683. NodeS2* curPtr = result_node_ptr;
  684. while(curPtr != NULL)
  685. {
  686. curPtr = curPtr->parent_node;
  687. numOfNode++;
  688. }
  689. sys_yield(); // update cursor position
  690. //-----------------------------------------
  691. // otherwise, there are more than one node
  692. //-----------------------------------------
  693. ResultNode* maxSizeResultNodeArray; // store all the result node in the reverse order, the turning point will be extracted later
  694. int nodeAllocated, nodeCount=0;
  695. int preNodeCount = nodeCount;
  696. nodeAllocated = numOfNode*2+2;
  697. maxSizeResultNodeArray = (ResultNode*) mem_add(nodeAllocated*sizeof(ResultNode));
  698. max_size_result_node_ptr = maxSizeResultNodeArray;
  699. parent_result_node_ptr = maxSizeResultNodeArray;
  700. //----------------------------------
  701. // start from the destination
  702. //----------------------------------
  703. memset(max_size_result_node_ptr, 0, sizeof(ResultNode)*nodeAllocated);
  704. int upper_left_x = nodePtr->node_x<<1;
  705. int upper_left_y = nodePtr->node_y<<1;
  706. if(!useClosestNode && (search_mode_s2==SEARCH_MODE_TO_ATTACK || can_move_to_s2(final_dest_x_s2, final_dest_y_s2)))
  707. {
  708. max_size_result_node_ptr->node_x = final_dest_x_s2;
  709. max_size_result_node_ptr->node_y = final_dest_y_s2;
  710. max_size_result_node_ptr++; // note: parent_result_node_ptr don't change here
  711. nodeCount++;
  712. //---------------------------------------------------
  713. // process the end node, may pass through two points
  714. //---------------------------------------------------
  715. if(search_mode_s2==SEARCH_MODE_REUSE)
  716. process_end_node(upper_left_x, upper_left_y, nodePtr->node_type, nodePtr->enter_direction, nodeCount);
  717. }
  718. else // closest node is used
  719. {
  720. //------------------------------------------------------------------------//
  721. // may generate duplicated node for the destination if closet node is used.
  722. //------------------------------------------------------------------------//
  723. switch(nodePtr->enter_direction)
  724. {
  725. case 1: max_size_result_node_ptr->node_x = upper_left_x-1;
  726. max_size_result_node_ptr->node_y = upper_left_y;
  727. break;
  728. case 2: max_size_result_node_ptr->node_x = upper_left_x-1;
  729. max_size_result_node_ptr->node_y = upper_left_y+1;
  730. break;
  731. case 3: max_size_result_node_ptr->node_x = upper_left_x;
  732. max_size_result_node_ptr->node_y = upper_left_y+1;
  733. break;
  734. case 4: max_size_result_node_ptr->node_x = upper_left_x+1;
  735. max_size_result_node_ptr->node_y = upper_left_y+1;
  736. break;
  737. case 5: max_size_result_node_ptr->node_x = upper_left_x+1;
  738. max_size_result_node_ptr->node_y = upper_left_y;
  739. break;
  740. case 6: max_size_result_node_ptr->node_x = upper_left_x+1;
  741. max_size_result_node_ptr->node_y = upper_left_y-1;
  742. break;
  743. case 7: max_size_result_node_ptr->node_x = upper_left_x;
  744. max_size_result_node_ptr->node_y = upper_left_y-1;
  745. break;
  746. case 8: max_size_result_node_ptr->node_x = upper_left_x-1;
  747. max_size_result_node_ptr->node_y = upper_left_y-1;
  748. break;
  749. default: err_here();
  750. break;
  751. }
  752. max_size_result_node_ptr++; // note: parent_result_node_ptr don't change here
  753. nodeCount++;
  754. }
  755. nodePtr = nodePtr->parent_node; // next 2x2 node
  756. //--------------------------------------------------
  757. // get the actual path, process from the second node
  758. //--------------------------------------------------
  759. int yieldCount = 0;
  760. while( (parentNode=nodePtr->parent_node) != NULL )
  761. {
  762. upper_left_x = nodePtr->node_x<<1;
  763. upper_left_y = nodePtr->node_y<<1;
  764. err_when(nodePtr->enter_direction==(childNodePtr->enter_direction+3)%8+1);
  765. get_real_result_node(nodeCount, nodePtr->enter_direction, (childNodePtr->enter_direction+3)%8+1,
  766. nodePtr->node_type, upper_left_x, upper_left_y);
  767. childNodePtr = nodePtr;
  768. nodePtr = parentNode;
  769. //err_when(nodeCount+nodePtr->node_g+2 > nodeAllocated);
  770. yieldCount++;
  771. if(yieldCount%20==0)
  772. sys_yield(); // update cursor position
  773. }
  774. sys_yield(); // update cursor position
  775. debug_check_paths2(maxSizeResultNodeArray, nodeCount);
  776. /*#ifdef DEBUG
  777. debugPtr1 = maxSizeResultNodeArray;
  778. debugPtr2 = maxSizeResultNodeArray+1;
  779. for(debugCount=1; debugCount<nodeCount-1; debugCount++)
  780. {
  781. debugVX = debugPtr2->node_x - debugPtr1->node_x;
  782. debugVY = debugPtr2->node_y - debugPtr1->node_y;
  783. err_when(debugVX!=0 && debugVY!=0 && abs(debugVX)!=abs(debugVY));
  784. debugPtr1++;
  785. debugPtr2++;
  786. }
  787. #endif*/
  788. //----------------------------------------------------
  789. // process the starting node
  790. // nodePtr points at the starting node now
  791. //----------------------------------------------------
  792. err_when(nodePtr->enter_direction); // it should be zero
  793. upper_left_x = nodePtr->node_x<<1;
  794. upper_left_y = nodePtr->node_y<<1;
  795. //------------------------------------------------------------------------------------------//
  796. // process the starting node
  797. //------------------------------------------------------------------------------------------//
  798. process_start_node(upper_left_x, upper_left_y, nodePtr->node_type, (childNodePtr->enter_direction+3)%8+1, nodeCount);
  799. max_size_result_node_ptr->node_x = vir_start_x_s2;
  800. max_size_result_node_ptr->node_y = vir_start_y_s2;
  801. err_when((abs(max_size_result_node_ptr->node_x-parent_result_node_ptr->node_x)>1) ||
  802. (abs(max_size_result_node_ptr->node_y-parent_result_node_ptr->node_y)>1));
  803. nodeCount++;
  804. sys_yield(); // update cursor position
  805. debug_check_paths2(maxSizeResultNodeArray, nodeCount);
  806. /*#ifdef DEBUG
  807. debugPtr1 = maxSizeResultNodeArray;
  808. debugPtr2 = maxSizeResultNodeArray+1;
  809. for(debugCount=1; debugCount<nodeCount; debugCount++)
  810. {
  811. debugVX = debugPtr2->node_x - debugPtr1->node_x;
  812. debugVY = debugPtr2->node_y - debugPtr1->node_y;
  813. err_when(debugVX!=0 && debugVY!=0 && (abs(debugVX)!=abs(debugVY)));
  814. debugPtr1++;
  815. debugPtr2++;
  816. }
  817. #endif*/
  818. //--------------------------------------------------//
  819. // smooth the path
  820. //--------------------------------------------------//
  821. maxSizeResultNodeArray = smooth_the_path(maxSizeResultNodeArray, nodeCount);
  822. sys_yield(); // update cursor position
  823. parent_result_node_ptr = maxSizeResultNodeArray;
  824. max_size_result_node_ptr = maxSizeResultNodeArray+1;
  825. ResultNode* result_node_pointer = max_size_result_node_ptr;
  826. //----------------------------------
  827. // get the turning point
  828. //----------------------------------
  829. short vectorX = max_size_result_node_ptr->node_x - parent_result_node_ptr->node_x;
  830. short vectorY = max_size_result_node_ptr->node_y - parent_result_node_ptr->node_y;
  831. short newVectorX, newVectorY;
  832. for(int i=1; i<nodeCount-1; i++)
  833. {
  834. parent_result_node_ptr = max_size_result_node_ptr; // don't use parent_result_node_ptr++, if the above code of removing duplication is used.
  835. max_size_result_node_ptr++;
  836. newVectorX=(max_size_result_node_ptr->node_x-parent_result_node_ptr->node_x);
  837. newVectorY=(max_size_result_node_ptr->node_y-parent_result_node_ptr->node_y);
  838. err_when(newVectorY!=0 && newVectorX!=0 && abs(newVectorX)!=abs(newVectorY));
  839. //------ turning to another direction at this point ------//
  840. if(vectorX!=newVectorX || vectorY!=newVectorY)
  841. {
  842. if(newVectorX!=0 || newVectorY!=0)
  843. {
  844. result_node_pointer->node_x = parent_result_node_ptr->node_x;
  845. result_node_pointer->node_y = parent_result_node_ptr->node_y;
  846. result_node_pointer++;
  847. resultNodeCount++;
  848. vectorX = newVectorX;
  849. vectorY = newVectorY;
  850. }
  851. }
  852. if(i%20==0)
  853. sys_yield(); // update cursor position
  854. }
  855. result_node_pointer->node_x = vir_start_x_s2;
  856. result_node_pointer->node_y = vir_start_y_s2;
  857. result_node_pointer++;
  858. resultNodeCount++;
  859. //------------------------------------------------//
  860. // After the above process, here we will have a
  861. // link of rationalize nodes. Retrieve them in the
  862. // backwards order
  863. //------------------------------------------------//
  864. ResultNode *resultNodeArray = (ResultNode*) mem_add( sizeof(ResultNode) * resultNodeCount );
  865. ResultNode *resultNodePtr = resultNodeArray+resultNodeCount-1;
  866. int processCount = 1;
  867. ResultNode *preNodePtr = maxSizeResultNodeArray;
  868. resultNodePtr->node_x = preNodePtr->node_x;
  869. resultNodePtr->node_y = preNodePtr->node_y;
  870. resultNodePtr--;
  871. result_node_pointer = maxSizeResultNodeArray+1;
  872. err_when(pathDist!=0);
  873. int xDist, yDist;
  874. while(processCount++ < resultNodeCount)
  875. {
  876. err_when(result_node_pointer->node_x<0 || result_node_pointer->node_x>=MAX_WORLD_X_LOC ||
  877. result_node_pointer->node_y<0 || result_node_pointer->node_y>=MAX_WORLD_Y_LOC);
  878. resultNodePtr->node_x = result_node_pointer->node_x;
  879. resultNodePtr->node_y = result_node_pointer->node_y;
  880. resultNodePtr--;
  881. xDist = abs(result_node_pointer->node_x-preNodePtr->node_x);
  882. yDist = abs(result_node_pointer->node_y-preNodePtr->node_y);
  883. err_when((!xDist && !yDist) || (xDist && yDist && xDist!=yDist));
  884. pathDist += (xDist) ? xDist : yDist;
  885. preNodePtr = result_node_pointer;
  886. result_node_pointer++;
  887. if(processCount%20==0)
  888. sys_yield(); // update cursor position
  889. }
  890. err_when(nodeAllocated<nodeCount);
  891. mem_del(maxSizeResultNodeArray);
  892. //-------- adjust/shift the path if shift-map-method is use --------//
  893. ResultNode* adjustNode = resultNodeArray;
  894. if(use_shift_map_method_s2)
  895. {
  896. for(int ii=0; ii<resultNodeCount; ii++)
  897. {
  898. adjustNode->node_x += shift_map_x_offset_s2;
  899. adjustNode->node_y += shift_map_y_offset_s2;
  900. adjustNode++;
  901. }
  902. }
  903. debug_check_paths2(resultNodeArray, resultNodeCount);
  904. /*#ifdef DEBUG
  905. debugPtr1 = resultNodeArray;
  906. debugPtr2 = resultNodeArray+1;
  907. for(debugCount=1; debugCount<resultNodeCount; debugCount++)
  908. {
  909. debugVX = debugPtr2->node_x - debugPtr1->node_x;
  910. debugVY = debugPtr2->node_y - debugPtr1->node_y;
  911. err_when(debugVX!=0 && debugVY!=0 && (abs(debugVX)!=abs(debugVY)));
  912. debugPtr1++;
  913. debugPtr2++;
  914. }
  915. #endif*/
  916. //--------------------------------------------------//
  917. // return NULL if there are only two nodes and these
  918. // nodes are equal
  919. //--------------------------------------------------//
  920. if(resultNodeCount == 2)
  921. {
  922. ResultNode *curTestNode1 = resultNodeArray;
  923. ResultNode *curTestNode2 = resultNodeArray+1;
  924. if(curTestNode1->node_x==curTestNode2->node_x &&
  925. curTestNode1->node_y==curTestNode2->node_y)
  926. {
  927. mem_del(resultNodeArray);
  928. resultNodeCount = 0;
  929. pathDist = 0;
  930. return NULL;
  931. }
  932. }
  933. #ifdef DEBUG
  934. if(resultNodeCount==2)
  935. {
  936. debugPtr1 = resultNodeArray;
  937. debugPtr2 = resultNodeArray+1;
  938. err_when(debugPtr1->node_x==debugPtr2->node_x && debugPtr1->node_y==debugPtr2->node_y);
  939. }
  940. #endif
  941. sys_yield(); // update cursor position
  942. return resultNodeArray;
  943. }
  944. //------ End of function SeekPathS2::get_result ---------//
  945. //---- Begin of function SeekPathS2::process_end_node ---------//
  946. void SeekPathS2::process_end_node(int xLoc, int yLoc, char nodeType, char enterDirection, int &nodeCount)
  947. {
  948. short usePathSuccess = 0; // if failure, use the last path in the checking
  949. short destType = 1 + (final_dest_x_s2%2) + 2*(final_dest_y_s2%2);
  950. switch(destType)
  951. {
  952. case 1:
  953. break;
  954. case 2:
  955. switch(enterDirection)
  956. {
  957. case 1:
  958. max_size_result_node_ptr->node_x = xLoc;
  959. max_size_result_node_ptr->node_y = yLoc;
  960. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  961. break;
  962. case 2:
  963. err_when(!(nodeType&0x1) && !can_move_to(xLoc+1,yLoc+2));
  964. max_size_result_node_ptr->node_x = xLoc;
  965. max_size_result_node_ptr->node_y = yLoc+!(nodeType&0x1);
  966. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  967. break;
  968. case 3: case 4: case 6: case 7:
  969. break;
  970. case 5:
  971. break;
  972. case 8:
  973. err_when(!(nodeType&0x4) && !can_move_to(xLoc+1,yLoc-1));
  974. max_size_result_node_ptr->node_x = xLoc;
  975. max_size_result_node_ptr->node_y = yLoc-!(nodeType&0x4);
  976. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  977. break;
  978. default: err_here();
  979. break;
  980. }
  981. break;
  982. case 3:
  983. switch(enterDirection)
  984. {
  985. case 1: case 2: case 4: case 5:
  986. break;
  987. case 3:
  988. break;
  989. case 6:
  990. err_when(!(nodeType&0x1) && !can_move_to(xLoc+2,yLoc+1));
  991. max_size_result_node_ptr->node_x = xLoc+!(nodeType&0x1);
  992. max_size_result_node_ptr->node_y = yLoc;
  993. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  994. break;
  995. case 7:
  996. max_size_result_node_ptr->node_x = xLoc;
  997. max_size_result_node_ptr->node_y = yLoc;
  998. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  999. break;
  1000. case 8:
  1001. err_when(!(nodeType&0x2) && !can_move_to(xLoc-1,yLoc+1));
  1002. max_size_result_node_ptr->node_x = xLoc-!(nodeType&0x2);
  1003. max_size_result_node_ptr->node_y = yLoc;
  1004. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1005. break;
  1006. default: err_here();
  1007. break;
  1008. }
  1009. break;
  1010. case 4:
  1011. switch(enterDirection)
  1012. {
  1013. case 1:
  1014. err_when(!(nodeType&0x2) && !can_move_to(xLoc,yLoc+2));
  1015. max_size_result_node_ptr->node_x = xLoc;
  1016. max_size_result_node_ptr->node_y = yLoc+can_move_to(xLoc,yLoc+2);
  1017. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1018. break;
  1019. case 2:
  1020. max_size_result_node_ptr->node_x = xLoc;
  1021. max_size_result_node_ptr->node_y = yLoc+1;
  1022. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1023. break;
  1024. case 3: case 4: case 5:
  1025. break;
  1026. case 6:
  1027. max_size_result_node_ptr->node_x = xLoc+1;
  1028. max_size_result_node_ptr->node_y = yLoc;
  1029. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1030. break;
  1031. case 7:
  1032. err_when(!(nodeType&0x4) && !can_move_to(xLoc+2,yLoc));
  1033. max_size_result_node_ptr->node_x = xLoc+can_move_to(xLoc+2,yLoc);
  1034. max_size_result_node_ptr->node_y = yLoc;
  1035. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1036. break;
  1037. case 8:
  1038. max_size_result_node_ptr->node_x = xLoc;
  1039. max_size_result_node_ptr->node_y = yLoc;
  1040. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1041. break;
  1042. default: err_here();
  1043. break;
  1044. }
  1045. break;
  1046. default: err_here();
  1047. break;
  1048. }
  1049. }
  1050. //------ End of function SeekPathS2::process_end_node ---------//
  1051. //---- Begin of function SeekPathS2::process_start_node ---------//
  1052. void SeekPathS2::process_start_node(int xLoc, int yLoc, char nodeType, char exitDirection, int &nodeCount)
  1053. {
  1054. short usePathSuccess = 0; // if failure, use the last path in the checking
  1055. switch(source_locate_type)
  1056. {
  1057. case 1:
  1058. switch(exitDirection)
  1059. {
  1060. case 1: // 3 possible paths
  1061. if(parent_result_node_ptr->node_y<yLoc && can_move_to(xLoc-1,yLoc-1) &&
  1062. can_move_to(xLoc,yLoc-1))
  1063. {
  1064. max_size_result_node_ptr->node_x = xLoc-1;
  1065. max_size_result_node_ptr->node_y = yLoc-1;
  1066. usePathSuccess++;
  1067. }
  1068. if(!usePathSuccess && parent_result_node_ptr->node_y>yLoc &&
  1069. can_move_to(xLoc-1,yLoc+2) && can_move_to(xLoc,yLoc+2))
  1070. {
  1071. max_size_result_node_ptr->node_x = xLoc-1;
  1072. max_size_result_node_ptr->node_y = yLoc+1;
  1073. usePathSuccess++;
  1074. }
  1075. if(!usePathSuccess)
  1076. {
  1077. max_size_result_node_ptr->node_x = xLoc-1;
  1078. max_size_result_node_ptr->node_y = yLoc;
  1079. }
  1080. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1081. break;
  1082. case 2: // 3 possible paths
  1083. if(parent_result_node_ptr->node_x==xLoc && //can_move_to(xLoc+1,yLoc+1) &&
  1084. can_move_to(xLoc+1,yLoc+2))
  1085. {
  1086. max_size_result_node_ptr->node_x = xLoc;
  1087. max_size_result_node_ptr->node_y = yLoc+1;
  1088. usePathSuccess++;
  1089. }
  1090. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc &&
  1091. can_move_to(xLoc-1,yLoc))
  1092. {
  1093. max_size_result_node_ptr->node_x = xLoc-1;
  1094. max_size_result_node_ptr->node_y = yLoc;
  1095. usePathSuccess++;
  1096. }
  1097. if(!usePathSuccess)
  1098. {
  1099. max_size_result_node_ptr->node_x = xLoc-1;
  1100. max_size_result_node_ptr->node_y = yLoc+1;
  1101. }
  1102. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1103. break;
  1104. case 3:
  1105. if(parent_result_node_ptr->node_x>xLoc && can_move_to(xLoc+2,yLoc+1) &&
  1106. can_move_to(xLoc+2,yLoc+2))
  1107. {
  1108. max_size_result_node_ptr->node_x = xLoc+1;
  1109. max_size_result_node_ptr->node_y = yLoc+1;
  1110. usePathSuccess++;
  1111. }
  1112. if(!usePathSuccess && parent_result_node_ptr->node_x<xLoc &&
  1113. can_move_to(xLoc-1,yLoc+1) && can_move_to(xLoc-1,yLoc+2))
  1114. {
  1115. max_size_result_node_ptr->node_x = xLoc-1;
  1116. max_size_result_node_ptr->node_y = yLoc+1;
  1117. usePathSuccess++;
  1118. }
  1119. if(!usePathSuccess)
  1120. {
  1121. max_size_result_node_ptr->node_x = xLoc;
  1122. max_size_result_node_ptr->node_y = yLoc+1;
  1123. }
  1124. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1125. break;
  1126. case 4:
  1127. if(parent_result_node_ptr->node_x==xLoc && //can_move_to(xLoc,yLoc+1) &&
  1128. can_move_to(xLoc,yLoc+2))
  1129. {
  1130. max_size_result_node_ptr->node_x = xLoc;
  1131. max_size_result_node_ptr->node_y = yLoc+1;
  1132. usePathSuccess++;
  1133. }
  1134. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc && //can_move_to(xLoc+1,yLoc) &&
  1135. can_move_to(xLoc+2,yLoc))
  1136. {
  1137. max_size_result_node_ptr->node_x = xLoc+1;
  1138. max_size_result_node_ptr->node_y = yLoc;
  1139. usePathSuccess++;
  1140. }
  1141. if(!usePathSuccess)
  1142. {
  1143. max_size_result_node_ptr->node_x = xLoc+1;
  1144. max_size_result_node_ptr->node_y = yLoc+1;
  1145. }
  1146. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1147. break;
  1148. case 5:
  1149. if(parent_result_node_ptr->node_y<yLoc && can_move_to(xLoc+1,yLoc-1) &&
  1150. can_move_to(xLoc+2,yLoc-1))
  1151. {
  1152. max_size_result_node_ptr->node_x = xLoc+1;
  1153. max_size_result_node_ptr->node_y = yLoc-1;
  1154. usePathSuccess++;
  1155. }
  1156. if(!usePathSuccess && parent_result_node_ptr->node_y>yLoc &&
  1157. can_move_to(xLoc+1,yLoc+2) && can_move_to(xLoc+2,yLoc+2))
  1158. {
  1159. max_size_result_node_ptr->node_x = xLoc+1;
  1160. max_size_result_node_ptr->node_y = yLoc+1;
  1161. usePathSuccess++;
  1162. }
  1163. if(!usePathSuccess)
  1164. {
  1165. max_size_result_node_ptr->node_x = xLoc+1;
  1166. max_size_result_node_ptr->node_y = yLoc;
  1167. }
  1168. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1169. break;
  1170. case 6:
  1171. if(parent_result_node_ptr->node_y==yLoc && //can_move_to(xLoc+1,yLoc+1) &&
  1172. can_move_to(xLoc+2,yLoc+1))
  1173. {
  1174. max_size_result_node_ptr->node_x = xLoc+1;
  1175. max_size_result_node_ptr->node_y = yLoc;
  1176. usePathSuccess++;
  1177. }
  1178. if(!usePathSuccess && parent_result_node_ptr->node_x==xLoc && can_move_to(xLoc,yLoc-1))
  1179. {
  1180. max_size_result_node_ptr->node_x = xLoc;
  1181. max_size_result_node_ptr->node_y = yLoc-1;
  1182. usePathSuccess++;
  1183. }
  1184. if(!usePathSuccess)
  1185. {
  1186. max_size_result_node_ptr->node_x = xLoc+1;
  1187. max_size_result_node_ptr->node_y = yLoc-1;
  1188. }
  1189. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1190. break;
  1191. case 7:
  1192. if(parent_result_node_ptr->node_x>xLoc && can_move_to(xLoc+2,yLoc-1) &&
  1193. can_move_to(xLoc+2,yLoc))
  1194. {
  1195. max_size_result_node_ptr->node_x = xLoc+1;
  1196. max_size_result_node_ptr->node_y = yLoc-1;
  1197. usePathSuccess++;
  1198. }
  1199. if(!usePathSuccess && parent_result_node_ptr->node_x<xLoc &&
  1200. can_move_to(xLoc-1,yLoc-1) && can_move_to(xLoc-1,yLoc))
  1201. {
  1202. max_size_result_node_ptr->node_x = xLoc-1;
  1203. max_size_result_node_ptr->node_y = yLoc-1;
  1204. usePathSuccess++;
  1205. }
  1206. if(!usePathSuccess)
  1207. {
  1208. max_size_result_node_ptr->node_x = xLoc;
  1209. max_size_result_node_ptr->node_y = yLoc-1;
  1210. }
  1211. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1212. break;
  1213. case 8:
  1214. if(parent_result_node_ptr->node_x==xLoc && can_move_to(xLoc+1,yLoc-1))
  1215. {
  1216. max_size_result_node_ptr->node_x = xLoc;
  1217. max_size_result_node_ptr->node_y = yLoc-1;
  1218. usePathSuccess++;
  1219. }
  1220. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc && can_move_to(xLoc-1,yLoc+1))
  1221. {
  1222. max_size_result_node_ptr->node_x = xLoc-1;
  1223. max_size_result_node_ptr->node_y = yLoc;
  1224. usePathSuccess++;
  1225. }
  1226. if(!usePathSuccess)
  1227. {
  1228. max_size_result_node_ptr->node_x = xLoc-1;
  1229. max_size_result_node_ptr->node_y = yLoc-1;
  1230. }
  1231. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1232. break;
  1233. default: err_here();
  1234. break;
  1235. }
  1236. break;
  1237. case 2:
  1238. switch(exitDirection)
  1239. {
  1240. case 1:
  1241. if(parent_result_node_ptr->node_y<yLoc && can_move_to(xLoc-1,yLoc-1) &&
  1242. can_move_to(xLoc,yLoc-1) && can_move_to(xLoc+1,yLoc-1))
  1243. {
  1244. max_size_result_node_ptr->node_x = xLoc-1;
  1245. max_size_result_node_ptr->node_y = yLoc-1;
  1246. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1247. max_size_result_node_ptr->node_x = xLoc;
  1248. max_size_result_node_ptr->node_y = yLoc-1;
  1249. usePathSuccess++;
  1250. }
  1251. if(!usePathSuccess && parent_result_node_ptr->node_y>yLoc && can_move_to(xLoc-1,yLoc+2) &&
  1252. can_move_to(xLoc,yLoc+2) && can_move_to(xLoc+1,yLoc+2))
  1253. {
  1254. max_size_result_node_ptr->node_x = xLoc-1;
  1255. max_size_result_node_ptr->node_y = yLoc+1;
  1256. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1257. max_size_result_node_ptr->node_x = xLoc;
  1258. max_size_result_node_ptr->node_y = yLoc+1;
  1259. usePathSuccess++;
  1260. }
  1261. if(!usePathSuccess)
  1262. {
  1263. max_size_result_node_ptr->node_x = xLoc-1;
  1264. max_size_result_node_ptr->node_y = yLoc;
  1265. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1266. max_size_result_node_ptr->node_x = xLoc;
  1267. max_size_result_node_ptr->node_y = yLoc;
  1268. }
  1269. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1270. break;
  1271. case 2:
  1272. if(parent_result_node_ptr->node_y==yLoc+1 && can_move_to(xLoc+1,yLoc+2))
  1273. {
  1274. max_size_result_node_ptr->node_x = xLoc-1;
  1275. max_size_result_node_ptr->node_y = yLoc+1;
  1276. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1277. max_size_result_node_ptr->node_x = xLoc;
  1278. max_size_result_node_ptr->node_y = yLoc+1;
  1279. usePathSuccess++;
  1280. }
  1281. if(!usePathSuccess && can_move_to(xLoc+1,yLoc+2) &&
  1282. ( (vir_start_x_s2-parent_result_node_ptr->node_x==parent_result_node_ptr->node_y-vir_start_y_s2) ||
  1283. (parent_result_node_ptr->node_x==xLoc) ) )
  1284. {
  1285. max_size_result_node_ptr->node_x = xLoc;
  1286. max_size_result_node_ptr->node_y = yLoc+1;
  1287. usePathSuccess++;
  1288. }
  1289. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc &&
  1290. can_move_to(xLoc-1,yLoc) && can_move_to(xLoc,yLoc))
  1291. {
  1292. max_size_result_node_ptr->node_x = xLoc-1;
  1293. max_size_result_node_ptr->node_y = yLoc;
  1294. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1295. max_size_result_node_ptr->node_x = xLoc;
  1296. max_size_result_node_ptr->node_y = yLoc;
  1297. usePathSuccess++;
  1298. }
  1299. if(!usePathSuccess)
  1300. {
  1301. max_size_result_node_ptr->node_x = xLoc-1;
  1302. max_size_result_node_ptr->node_y = yLoc+1;
  1303. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1304. err_when(!(nodeType&0x1) && !can_move_to(xLoc+1,yLoc+2));
  1305. max_size_result_node_ptr->node_x = xLoc;
  1306. max_size_result_node_ptr->node_y = yLoc+(!(nodeType&0x1));
  1307. }
  1308. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1309. break;
  1310. case 3:
  1311. if(parent_result_node_ptr->node_x>xLoc && //can_move_to(xLoc+2,yLoc+1) &&
  1312. can_move_to(xLoc+2,yLoc+2))
  1313. {
  1314. max_size_result_node_ptr->node_x = xLoc+1;
  1315. max_size_result_node_ptr->node_y = yLoc+1;
  1316. usePathSuccess++;
  1317. }
  1318. if(!usePathSuccess)
  1319. {
  1320. max_size_result_node_ptr->node_x = xLoc;
  1321. max_size_result_node_ptr->node_y = yLoc+1;
  1322. }
  1323. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1324. break;
  1325. case 4:
  1326. if(parent_result_node_ptr->node_x==xLoc && can_move_to(xLoc,yLoc+1) &&
  1327. can_move_to(xLoc,yLoc+2))
  1328. {
  1329. max_size_result_node_ptr->node_x = xLoc;
  1330. max_size_result_node_ptr->node_y = yLoc+1;
  1331. usePathSuccess++;
  1332. }
  1333. if(!usePathSuccess)
  1334. {
  1335. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1336. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1337. {
  1338. max_size_result_node_ptr->node_x = xLoc+1;
  1339. max_size_result_node_ptr->node_y = yLoc+1;
  1340. }
  1341. else
  1342. break;
  1343. }
  1344. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1345. break;
  1346. case 5:
  1347. break;
  1348. case 6:
  1349. if(parent_result_node_ptr->node_x==xLoc && can_move_to(xLoc,yLoc-1) &&
  1350. can_move_to(xLoc,yLoc))
  1351. {
  1352. max_size_result_node_ptr->node_x = xLoc;
  1353. max_size_result_node_ptr->node_y = yLoc-1;
  1354. usePathSuccess++;
  1355. }
  1356. if(!usePathSuccess)
  1357. {
  1358. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1359. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1360. {
  1361. max_size_result_node_ptr->node_x = xLoc+1;
  1362. max_size_result_node_ptr->node_y = yLoc-1;
  1363. }
  1364. else
  1365. break;
  1366. }
  1367. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1368. break;
  1369. case 7:
  1370. if(parent_result_node_ptr->node_x>xLoc && can_move_to(xLoc+2,yLoc-1) &&
  1371. can_move_to(xLoc+2,yLoc))
  1372. {
  1373. max_size_result_node_ptr->node_x = xLoc+1;
  1374. max_size_result_node_ptr->node_y = yLoc-1;
  1375. usePathSuccess++;
  1376. }
  1377. if(!usePathSuccess)
  1378. {
  1379. max_size_result_node_ptr->node_x = xLoc;
  1380. max_size_result_node_ptr->node_y = yLoc-1;
  1381. }
  1382. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1383. break;
  1384. case 8:
  1385. if(parent_result_node_ptr->node_y==yLoc-1 && can_move_to(xLoc+1,yLoc-1))
  1386. {
  1387. max_size_result_node_ptr->node_x = xLoc-1;
  1388. max_size_result_node_ptr->node_y = yLoc-1;
  1389. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1390. max_size_result_node_ptr->node_x = xLoc;
  1391. max_size_result_node_ptr->node_y = yLoc-1;
  1392. usePathSuccess++;
  1393. }
  1394. if(!usePathSuccess && can_move_to(xLoc+1,yLoc-1) &&
  1395. ( (vir_start_x_s2-parent_result_node_ptr->node_x==vir_start_y_s2-parent_result_node_ptr->node_y) ||
  1396. (parent_result_node_ptr->node_x==xLoc) ) )
  1397. {
  1398. max_size_result_node_ptr->node_x = xLoc;
  1399. max_size_result_node_ptr->node_y = yLoc-1;
  1400. usePathSuccess++;
  1401. }
  1402. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc &&
  1403. can_move_to(xLoc-1,yLoc+1) && can_move_to(xLoc,yLoc+1))
  1404. {
  1405. max_size_result_node_ptr->node_x = xLoc-1;
  1406. max_size_result_node_ptr->node_y = yLoc;
  1407. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1408. max_size_result_node_ptr->node_x = xLoc;
  1409. max_size_result_node_ptr->node_y = yLoc;
  1410. usePathSuccess++;
  1411. }
  1412. if(!usePathSuccess)
  1413. {
  1414. max_size_result_node_ptr->node_x = xLoc-1;
  1415. max_size_result_node_ptr->node_y = yLoc-1;
  1416. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1417. err_when(!(nodeType&0x4) && !can_move_to(xLoc+1,yLoc-1));
  1418. max_size_result_node_ptr->node_x = xLoc;
  1419. max_size_result_node_ptr->node_y = yLoc-(!(nodeType&0x4));
  1420. }
  1421. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1422. break;
  1423. default: err_here();
  1424. break;
  1425. }
  1426. break;
  1427. case 3:
  1428. switch(exitDirection)
  1429. {
  1430. case 1:
  1431. if(parent_result_node_ptr->node_y>yLoc && can_move_to(xLoc-1,yLoc+2))
  1432. {
  1433. max_size_result_node_ptr->node_x = xLoc-1;
  1434. max_size_result_node_ptr->node_y = yLoc+1;
  1435. usePathSuccess++;
  1436. }
  1437. if(!usePathSuccess)
  1438. {
  1439. max_size_result_node_ptr->node_x = xLoc-1;
  1440. max_size_result_node_ptr->node_y = yLoc;
  1441. }
  1442. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1443. break;
  1444. case 2:
  1445. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1446. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1447. {
  1448. max_size_result_node_ptr->node_x = xLoc-1;
  1449. max_size_result_node_ptr->node_y = yLoc+1;
  1450. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1451. }
  1452. break;
  1453. case 3:
  1454. break;
  1455. case 4:
  1456. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1457. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1458. {
  1459. max_size_result_node_ptr->node_x = xLoc+1;
  1460. max_size_result_node_ptr->node_y = yLoc+1;
  1461. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1462. }
  1463. break;
  1464. case 5:
  1465. if(parent_result_node_ptr->node_y>yLoc && can_move_to(xLoc+2,yLoc+2))
  1466. {
  1467. max_size_result_node_ptr->node_x = xLoc+1;
  1468. max_size_result_node_ptr->node_y = yLoc+1;
  1469. usePathSuccess++;
  1470. }
  1471. if(!usePathSuccess)
  1472. {
  1473. max_size_result_node_ptr->node_x = xLoc+1;
  1474. max_size_result_node_ptr->node_y = yLoc;
  1475. }
  1476. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1477. break;
  1478. case 6:
  1479. if(parent_result_node_ptr->node_y==yLoc && can_move_to(xLoc+2,yLoc+1))
  1480. {
  1481. max_size_result_node_ptr->node_x = xLoc+1;
  1482. max_size_result_node_ptr->node_y = yLoc;
  1483. usePathSuccess++;
  1484. }
  1485. if(!usePathSuccess && parent_result_node_ptr->node_x==xLoc &&
  1486. can_move_to(xLoc,yLoc-1) && can_move_to(xLoc,yLoc))
  1487. {
  1488. max_size_result_node_ptr->node_x = xLoc;
  1489. max_size_result_node_ptr->node_y = yLoc-1;
  1490. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1491. max_size_result_node_ptr->node_x = xLoc;
  1492. max_size_result_node_ptr->node_y = yLoc;
  1493. usePathSuccess++;
  1494. }
  1495. if(!usePathSuccess)
  1496. {
  1497. max_size_result_node_ptr->node_x = xLoc+1;
  1498. max_size_result_node_ptr->node_y = yLoc-1;
  1499. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1500. err_when(!(nodeType&0x1) && !can_move_to(xLoc+2, yLoc+1));
  1501. max_size_result_node_ptr->node_x = xLoc+!(nodeType&0x1);
  1502. max_size_result_node_ptr->node_y = yLoc;
  1503. }
  1504. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1505. break;
  1506. case 7:
  1507. if(parent_result_node_ptr->node_x>xLoc && can_move_to(xLoc+2,yLoc-1) &&
  1508. can_move_to(xLoc+2,yLoc))
  1509. {
  1510. max_size_result_node_ptr->node_x = xLoc+1;
  1511. max_size_result_node_ptr->node_y = yLoc-1;
  1512. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1513. max_size_result_node_ptr->node_x = xLoc+1;
  1514. max_size_result_node_ptr->node_y = yLoc;
  1515. usePathSuccess++;
  1516. }
  1517. if(!usePathSuccess && parent_result_node_ptr->node_x<xLoc &&
  1518. can_move_to(xLoc-1,yLoc-1) && can_move_to(xLoc-1,yLoc))
  1519. {
  1520. max_size_result_node_ptr->node_x = xLoc-1;
  1521. max_size_result_node_ptr->node_y = yLoc-1;
  1522. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1523. max_size_result_node_ptr->node_x = xLoc-1;
  1524. max_size_result_node_ptr->node_y = yLoc;
  1525. usePathSuccess++;
  1526. }
  1527. if(!usePathSuccess)
  1528. {
  1529. max_size_result_node_ptr->node_x = xLoc;
  1530. max_size_result_node_ptr->node_y = yLoc-1;
  1531. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1532. max_size_result_node_ptr->node_x = xLoc;
  1533. max_size_result_node_ptr->node_y = yLoc;
  1534. }
  1535. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1536. break;
  1537. case 8:
  1538. if(can_move_to(xLoc-1,yLoc+1) &&
  1539. ( (vir_start_x_s2-parent_result_node_ptr->node_x==vir_start_y_s2-parent_result_node_ptr->node_y)||
  1540. (parent_result_node_ptr->node_y==yLoc) ) )
  1541. {
  1542. max_size_result_node_ptr->node_x = xLoc-1;
  1543. max_size_result_node_ptr->node_y = yLoc;
  1544. usePathSuccess++;
  1545. }
  1546. if(!usePathSuccess && parent_result_node_ptr->node_x==xLoc &&
  1547. can_move_to(xLoc+1,yLoc-1) && can_move_to(xLoc+1,yLoc))
  1548. {
  1549. max_size_result_node_ptr->node_x = xLoc;
  1550. max_size_result_node_ptr->node_y = yLoc-1;
  1551. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1552. max_size_result_node_ptr->node_x = xLoc;
  1553. max_size_result_node_ptr->node_y = yLoc;
  1554. usePathSuccess++;
  1555. }
  1556. if(!usePathSuccess)
  1557. {
  1558. max_size_result_node_ptr->node_x = xLoc-1;
  1559. max_size_result_node_ptr->node_y = yLoc-1;
  1560. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1561. err_when(!(nodeType&0x2) && !can_move_to(xLoc-1, yLoc+1));
  1562. max_size_result_node_ptr->node_x = xLoc-!(nodeType&0x2);
  1563. max_size_result_node_ptr->node_y = yLoc;
  1564. }
  1565. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1566. break;
  1567. default: err_here();
  1568. break;
  1569. }
  1570. break;
  1571. case 4:
  1572. switch(exitDirection)
  1573. {
  1574. case 1:
  1575. if(parent_result_node_ptr->node_y>yLoc && can_move_to(xLoc-1,yLoc+2) &&
  1576. can_move_to(xLoc,yLoc+2))
  1577. {
  1578. max_size_result_node_ptr->node_x = xLoc-1;
  1579. max_size_result_node_ptr->node_y = yLoc+1;
  1580. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1581. max_size_result_node_ptr->node_x = xLoc;
  1582. max_size_result_node_ptr->node_y = yLoc+1;
  1583. usePathSuccess++;
  1584. }
  1585. if(!usePathSuccess && can_move_to(xLoc,yLoc+2) && parent_result_node_ptr->node_y==yLoc-1)
  1586. {
  1587. max_size_result_node_ptr->node_x = xLoc-1;
  1588. max_size_result_node_ptr->node_y = yLoc;
  1589. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1590. max_size_result_node_ptr->node_x = xLoc;
  1591. max_size_result_node_ptr->node_y = yLoc+1;
  1592. usePathSuccess++;
  1593. }
  1594. if(!usePathSuccess)
  1595. {
  1596. max_size_result_node_ptr->node_x = xLoc-1;
  1597. max_size_result_node_ptr->node_y = yLoc;
  1598. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1599. err_when(!(nodeType&0x2) && !can_move_to(xLoc, yLoc+2));
  1600. max_size_result_node_ptr->node_x = xLoc;
  1601. max_size_result_node_ptr->node_y = yLoc+!(nodeType&0x2);
  1602. }
  1603. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1604. break;
  1605. case 2:
  1606. if(vir_start_x_s2-parent_result_node_ptr->node_x==parent_result_node_ptr->node_y+1-vir_start_y_s2)
  1607. {
  1608. max_size_result_node_ptr->node_x = xLoc;
  1609. max_size_result_node_ptr->node_y = yLoc+1;
  1610. usePathSuccess++;
  1611. }
  1612. if(!usePathSuccess)
  1613. {
  1614. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1615. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1616. {
  1617. max_size_result_node_ptr->node_x = xLoc-1;
  1618. max_size_result_node_ptr->node_y = yLoc+1;
  1619. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1620. max_size_result_node_ptr->node_x = xLoc;
  1621. max_size_result_node_ptr->node_y = yLoc+1;
  1622. }
  1623. else
  1624. break;
  1625. }
  1626. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1627. break;
  1628. case 3:
  1629. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1630. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1631. {
  1632. max_size_result_node_ptr->node_x = xLoc;
  1633. max_size_result_node_ptr->node_y = yLoc+1;
  1634. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1635. }
  1636. break;
  1637. case 4:
  1638. break;
  1639. case 5:
  1640. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1641. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1642. {
  1643. max_size_result_node_ptr->node_x = xLoc+1;
  1644. max_size_result_node_ptr->node_y = yLoc;
  1645. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1646. }
  1647. break;
  1648. case 6:
  1649. if(parent_result_node_ptr->node_x+1-vir_start_x_s2==vir_start_y_s2-parent_result_node_ptr->node_y)
  1650. {
  1651. max_size_result_node_ptr->node_x = xLoc+1;
  1652. max_size_result_node_ptr->node_y = yLoc;
  1653. usePathSuccess++;
  1654. }
  1655. if(!usePathSuccess)
  1656. {
  1657. if(abs(parent_result_node_ptr->node_x-vir_start_x_s2)>1 ||
  1658. abs(parent_result_node_ptr->node_y-vir_start_y_s2)>1)
  1659. {
  1660. max_size_result_node_ptr->node_x = xLoc+1;
  1661. max_size_result_node_ptr->node_y = yLoc-1;
  1662. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1663. max_size_result_node_ptr->node_x = xLoc+1;
  1664. max_size_result_node_ptr->node_y = yLoc;
  1665. }
  1666. else
  1667. break;
  1668. }
  1669. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1670. break;
  1671. case 7:
  1672. if(parent_result_node_ptr->node_x>xLoc && can_move_to(xLoc+2,yLoc-1) &&
  1673. can_move_to(xLoc+2,yLoc))
  1674. {
  1675. max_size_result_node_ptr->node_x = xLoc+1;
  1676. max_size_result_node_ptr->node_y = yLoc-1;
  1677. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1678. max_size_result_node_ptr->node_x = xLoc+1;
  1679. max_size_result_node_ptr->node_y = yLoc;
  1680. usePathSuccess++;
  1681. }
  1682. if(!usePathSuccess && parent_result_node_ptr->node_x<xLoc &&
  1683. can_move_to(xLoc+2,yLoc))
  1684. {
  1685. max_size_result_node_ptr->node_x = xLoc;
  1686. max_size_result_node_ptr->node_y = yLoc-1;
  1687. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1688. max_size_result_node_ptr->node_x = xLoc+1;
  1689. max_size_result_node_ptr->node_y = yLoc;
  1690. usePathSuccess++;
  1691. }
  1692. if(!usePathSuccess)
  1693. {
  1694. max_size_result_node_ptr->node_x = xLoc;
  1695. max_size_result_node_ptr->node_y = yLoc-1;
  1696. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1697. err_when(!(nodeType&0x4) && !can_move_to(xLoc+2, yLoc));
  1698. max_size_result_node_ptr->node_x = xLoc+!(nodeType&0x4);
  1699. max_size_result_node_ptr->node_y = yLoc;
  1700. }
  1701. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1702. break;
  1703. case 8:
  1704. if(parent_result_node_ptr->node_x==xLoc && can_move_to(xLoc+1,yLoc-1))
  1705. {
  1706. max_size_result_node_ptr->node_x = xLoc;
  1707. max_size_result_node_ptr->node_y = yLoc-1;
  1708. usePathSuccess++;
  1709. }
  1710. if(!usePathSuccess && parent_result_node_ptr->node_y==yLoc && can_move_to(xLoc-1,yLoc+1))
  1711. {
  1712. max_size_result_node_ptr->node_x = xLoc-1;
  1713. max_size_result_node_ptr->node_y = yLoc;
  1714. usePathSuccess++;
  1715. }
  1716. if(!usePathSuccess)
  1717. {
  1718. max_size_result_node_ptr->node_x = xLoc-1;
  1719. max_size_result_node_ptr->node_y = yLoc-1;
  1720. }
  1721. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1722. err_when(!can_move_to(xLoc+1, yLoc) || !can_move_to(xLoc, yLoc+1));
  1723. max_size_result_node_ptr->node_x = xLoc;
  1724. max_size_result_node_ptr->node_y = yLoc;
  1725. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, nodeCount);
  1726. break;
  1727. default: err_here();
  1728. break;
  1729. }
  1730. break;
  1731. default: err_here();
  1732. break;
  1733. }
  1734. }
  1735. //------ End of function SeekPathS2::process_start_node ---------//
  1736. //---- Begin of function SeekPathS2::get_real_result_node ---------//
  1737. //
  1738. // called by get_result to extract the point in a 2x2 node that is
  1739. // used in the shortest path
  1740. //
  1741. // <int&> count - a reference var for counting number of node in
  1742. // max_size_result_node_ptr
  1743. //
  1744. void SeekPathS2::get_real_result_node(int &count, short enterDirection, short exitDirection,
  1745. short nodeType, short xCoord, short yCoord)
  1746. {
  1747. prefer_upper_s2 = prefer_lower_s2 = prefer_left_s2 = prefer_right_s2 = 0;
  1748. switch(enterDirection)
  1749. {
  1750. case 0: err_here();
  1751. break;
  1752. case 1:
  1753. switch(exitDirection)
  1754. {
  1755. case 2: // 2 possible paths
  1756. if(parent_result_node_ptr->node_x<xCoord)
  1757. prefer_left_s2 = 1;
  1758. else
  1759. {
  1760. if(!can_move_to(xCoord+1,yCoord+1) || !can_move_to(xCoord+1,yCoord+2))
  1761. prefer_left_s2 = 1;
  1762. else
  1763. prefer_right_s2 = 1;
  1764. }
  1765. max_size_result_node_ptr->node_x = xCoord-prefer_left_s2;
  1766. max_size_result_node_ptr->node_y = yCoord+1;
  1767. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1768. break;
  1769. case 3: // 2 possible paths
  1770. if(parent_result_node_ptr->node_x>=xCoord)
  1771. prefer_right_s2 = 1;
  1772. else
  1773. {
  1774. if(can_move_to(xCoord-1,yCoord+2))
  1775. prefer_left_s2 = 1;
  1776. else
  1777. prefer_right_s2 = 1;
  1778. }
  1779. max_size_result_node_ptr->node_x = xCoord-prefer_left_s2;
  1780. max_size_result_node_ptr->node_y = yCoord+1;
  1781. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1782. break;
  1783. case 4: // 2 points, 2 paths
  1784. max_size_result_node_ptr->node_x = xCoord+1;
  1785. max_size_result_node_ptr->node_y = yCoord+1;
  1786. //----------- process path selection ---------------//
  1787. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  1788. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  1789. prefer_lower_s2 = 1;
  1790. else
  1791. prefer_upper_s2 = 1;
  1792. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1793. err_when(!(nodeType&0x2) && !can_move_to(xCoord, yCoord+2));
  1794. max_size_result_node_ptr->node_x = xCoord;
  1795. if(prefer_upper_s2)
  1796. max_size_result_node_ptr->node_y = yCoord + !(nodeType&0x2);
  1797. else // prefer_lower_s2
  1798. max_size_result_node_ptr->node_y = yCoord + can_move_to(xCoord, yCoord+2);
  1799. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  1800. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1801. break;
  1802. case 5: // 2 points, 2 paths
  1803. //----------- process path selection ---------------//
  1804. if(parent_result_node_ptr->node_y<=yCoord)
  1805. prefer_upper_s2 = 1;
  1806. else
  1807. {
  1808. if(!can_move_to(xCoord,yCoord+2) || !can_move_to(xCoord+1,yCoord+2) ||
  1809. !can_move_to(xCoord+2,yCoord+2))
  1810. prefer_upper_s2 = 1;
  1811. else
  1812. prefer_lower_s2 = 1;
  1813. }
  1814. max_size_result_node_ptr->node_x = xCoord+1;
  1815. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  1816. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  1817. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1818. max_size_result_node_ptr->node_x = xCoord;
  1819. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  1820. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  1821. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1822. break;
  1823. case 6: // 2 points, 2 paths
  1824. max_size_result_node_ptr->node_x = xCoord+1;
  1825. max_size_result_node_ptr->node_y = yCoord-1;
  1826. //----------- process path selection ---------------//
  1827. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  1828. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  1829. prefer_upper_s2 = 1;
  1830. else
  1831. prefer_lower_s2 = 1;
  1832. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1833. err_when(!(nodeType&0x8) && !can_move_to(xCoord, yCoord-1));
  1834. max_size_result_node_ptr->node_x = xCoord;
  1835. if(prefer_upper_s2)
  1836. max_size_result_node_ptr->node_y = yCoord-can_move_to(xCoord, yCoord-1);
  1837. else // prefer_lower_s2
  1838. max_size_result_node_ptr->node_y = yCoord-!(nodeType&0x8);
  1839. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord-1);
  1840. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1841. break;
  1842. case 7: // 2 possible paths
  1843. if(parent_result_node_ptr->node_x>=xCoord)
  1844. prefer_right_s2 = 1;
  1845. else
  1846. {
  1847. if(can_move_to(xCoord-1,yCoord-1))
  1848. prefer_left_s2 = 1;
  1849. else
  1850. prefer_right_s2 = 1;
  1851. }
  1852. max_size_result_node_ptr->node_x = xCoord-prefer_left_s2;
  1853. max_size_result_node_ptr->node_y = yCoord-1;
  1854. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1855. break;
  1856. case 8: // 2 possible paths
  1857. if(parent_result_node_ptr->node_x<xCoord)
  1858. prefer_left_s2 = 1;
  1859. else
  1860. {
  1861. if(!can_move_to(xCoord+1,yCoord-1) || !can_move_to(xCoord+1,yCoord))
  1862. prefer_left_s2 = 1;
  1863. else
  1864. prefer_right_s2 = 1;
  1865. }
  1866. max_size_result_node_ptr->node_x = xCoord-prefer_left_s2;
  1867. max_size_result_node_ptr->node_y = yCoord-1;
  1868. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1869. break;
  1870. default: err_here();
  1871. break;
  1872. }
  1873. break;
  1874. case 2:
  1875. switch(exitDirection)
  1876. {
  1877. case 1:
  1878. max_size_result_node_ptr->node_x = xCoord-1;
  1879. max_size_result_node_ptr->node_y = yCoord;
  1880. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1881. break;
  1882. case 3:
  1883. max_size_result_node_ptr->node_x = xCoord;
  1884. max_size_result_node_ptr->node_y = yCoord+1;
  1885. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1886. break;
  1887. case 4: // 2 points, 2 possible paths
  1888. if(parent_result_node_ptr->node_y<=yCoord+1)
  1889. prefer_upper_s2 = 1;
  1890. else
  1891. {
  1892. if(!can_move_to(xCoord, yCoord+3) || !can_move_to(xCoord+1, yCoord+3) ||
  1893. !can_move_to(xCoord+2, yCoord+3))
  1894. prefer_upper_s2 = 1;
  1895. else
  1896. prefer_lower_s2 = 1;
  1897. }
  1898. max_size_result_node_ptr->node_x = xCoord+1;
  1899. max_size_result_node_ptr->node_y = yCoord+1+prefer_lower_s2;
  1900. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1901. max_size_result_node_ptr->node_x = xCoord;
  1902. max_size_result_node_ptr->node_y = yCoord+1+prefer_lower_s2;
  1903. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1904. break;
  1905. case 5: // 2 points, 2 paths
  1906. if(parent_result_node_ptr->node_y<=yCoord)
  1907. prefer_upper_s2 = 1;
  1908. else
  1909. {
  1910. if(!can_move_to(xCoord+2,yCoord+2) || !can_move_to(xCoord+1,yCoord+2))
  1911. prefer_upper_s2 = 1;
  1912. else
  1913. prefer_lower_s2 = 1;
  1914. }
  1915. max_size_result_node_ptr->node_x = xCoord+1;
  1916. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  1917. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  1918. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1919. err_when(!(nodeType&0x1) && !can_move_to(xCoord+1, yCoord+2));
  1920. max_size_result_node_ptr->node_x = xCoord;
  1921. max_size_result_node_ptr->node_y = yCoord+(prefer_lower_s2 || !(nodeType&0x1));
  1922. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  1923. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1924. break;
  1925. case 6: // 2 points
  1926. max_size_result_node_ptr->node_x = xCoord+1;
  1927. max_size_result_node_ptr->node_y = yCoord-1;
  1928. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1929. err_when(nodeType != 15);
  1930. max_size_result_node_ptr->node_x = xCoord;
  1931. max_size_result_node_ptr->node_y = yCoord;
  1932. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1933. break;
  1934. case 7: // 2 points, 2 paths
  1935. max_size_result_node_ptr->node_x = xCoord;
  1936. max_size_result_node_ptr->node_y = yCoord-1;
  1937. //----------- process path selection ---------------//
  1938. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  1939. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  1940. prefer_right_s2 = 1;
  1941. else
  1942. prefer_left_s2 = 1;
  1943. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1944. err_when(!can_move_to(xCoord-1,yCoord) && !(nodeType&0x8));
  1945. max_size_result_node_ptr->node_y = yCoord;
  1946. if(prefer_left_s2)
  1947. max_size_result_node_ptr->node_x = xCoord-can_move_to(xCoord-1,yCoord);
  1948. else
  1949. max_size_result_node_ptr->node_x = xCoord-!(nodeType&0x8);
  1950. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord-1);
  1951. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1952. break;
  1953. case 8: // 2 points, 2 possible paths
  1954. if(parent_result_node_ptr->node_x>=xCoord-1)
  1955. prefer_right_s2 = 1;
  1956. else
  1957. {
  1958. if(!can_move_to(xCoord-2,yCoord-1) || !can_move_to(xCoord-2,yCoord) ||
  1959. !can_move_to(xCoord-2,yCoord+1)) // must check for all 3 points to avoid problems in dummy node generated in path-reuse
  1960. prefer_right_s2 = 1;
  1961. else
  1962. prefer_left_s2 = 1;
  1963. }
  1964. max_size_result_node_ptr->node_x = xCoord-1-prefer_left_s2;
  1965. max_size_result_node_ptr->node_y = yCoord-1;
  1966. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1967. max_size_result_node_ptr->node_x = xCoord-1-prefer_left_s2;
  1968. max_size_result_node_ptr->node_y = yCoord;
  1969. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1970. break;
  1971. default: err_here();
  1972. break;
  1973. }
  1974. break;
  1975. case 3: switch(exitDirection)
  1976. {
  1977. case 1: // 2 possible paths
  1978. if(parent_result_node_ptr->node_y<=yCoord)
  1979. prefer_upper_s2 = 1;
  1980. else
  1981. {
  1982. if(can_move_to(xCoord-1,yCoord+2))
  1983. prefer_lower_s2 = 1;
  1984. else
  1985. prefer_upper_s2 = 1;
  1986. }
  1987. max_size_result_node_ptr->node_x = xCoord-1;
  1988. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  1989. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  1990. break;
  1991. case 2: // 2 possible paths
  1992. if(parent_result_node_ptr->node_y>yCoord)
  1993. prefer_lower_s2 = 1;
  1994. else
  1995. {
  1996. if(!can_move_to(xCoord, yCoord) || !can_move_to(xCoord-1,yCoord))
  1997. prefer_lower_s2 = 1;
  1998. else
  1999. prefer_upper_s2 = 1;
  2000. }
  2001. max_size_result_node_ptr->node_x = xCoord-1;
  2002. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2003. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2004. break;
  2005. case 4: // 2 possible paths
  2006. if(parent_result_node_ptr->node_y>yCoord)
  2007. prefer_lower_s2 = 1;
  2008. else
  2009. {
  2010. if(!can_move_to(xCoord+1,yCoord) || !can_move_to(xCoord+2,yCoord))
  2011. prefer_lower_s2 = 1;
  2012. else
  2013. prefer_upper_s2 = 1;
  2014. }
  2015. max_size_result_node_ptr->node_x = xCoord+1;
  2016. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2017. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2018. break;
  2019. case 5: // 2 possible paths
  2020. if(parent_result_node_ptr->node_y<=yCoord)
  2021. prefer_upper_s2 = 1;
  2022. else
  2023. {
  2024. if(can_move_to(xCoord+2,yCoord+2))
  2025. prefer_lower_s2 = 1;
  2026. else
  2027. prefer_upper_s2 = 1;
  2028. }
  2029. max_size_result_node_ptr->node_x = xCoord+1;
  2030. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2031. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2032. break;
  2033. case 6: // 2 points. 2 paths
  2034. max_size_result_node_ptr->node_x = xCoord+1;
  2035. max_size_result_node_ptr->node_y = yCoord-1;
  2036. //----------- process path selection ---------------//
  2037. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2038. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2039. prefer_right_s2 = 1;
  2040. else
  2041. prefer_left_s2 = 1;
  2042. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2043. err_when(!(nodeType&0x1) && !can_move_to(xCoord+2, yCoord+1));
  2044. max_size_result_node_ptr->node_y = yCoord;
  2045. if(prefer_left_s2)
  2046. max_size_result_node_ptr->node_x = xCoord + !(nodeType&0x1);
  2047. else // prefer_right_s2
  2048. max_size_result_node_ptr->node_x = xCoord + can_move_to(xCoord+2, yCoord+1);
  2049. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2050. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2051. break;
  2052. case 7: // 2 points, 2 paths
  2053. max_size_result_node_ptr->node_y = yCoord-1;
  2054. //----------- process path selection ---------------//
  2055. if(parent_result_node_ptr->node_x<=xCoord)
  2056. prefer_left_s2 = 1;
  2057. else
  2058. {
  2059. if(!can_move_to(xCoord+2,yCoord-1) || !can_move_to(xCoord+2,yCoord) ||
  2060. !can_move_to(xCoord+2,yCoord+1))
  2061. prefer_left_s2 = 1;
  2062. else
  2063. prefer_right_s2 = 1;
  2064. }
  2065. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2066. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2067. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2068. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2069. max_size_result_node_ptr->node_y = yCoord;
  2070. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2071. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2072. break;
  2073. case 8: // 2 points, 2 paths
  2074. max_size_result_node_ptr->node_x = xCoord-1;
  2075. max_size_result_node_ptr->node_y = yCoord-1;
  2076. //----------- process path selection ---------------//
  2077. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2078. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2079. prefer_left_s2 = 1;
  2080. else
  2081. prefer_right_s2 = 1;
  2082. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2083. err_when(!(nodeType&0x2) && !can_move_to(xCoord-1, yCoord+1));
  2084. max_size_result_node_ptr->node_y = yCoord;
  2085. if(prefer_left_s2)
  2086. max_size_result_node_ptr->node_x = xCoord - can_move_to(xCoord-1, yCoord+1);
  2087. else // prefer_right_s2
  2088. max_size_result_node_ptr->node_x = xCoord - !(nodeType&0x2);
  2089. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord-1);
  2090. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2091. break;
  2092. default: err_here();
  2093. break;
  2094. }
  2095. break;
  2096. case 4: switch(exitDirection)
  2097. {
  2098. case 1: // 2 points, 2 paths
  2099. max_size_result_node_ptr->node_x = xCoord-1;
  2100. //----------- process path selection ---------------//
  2101. if(parent_result_node_ptr->node_y<=yCoord)
  2102. prefer_upper_s2 = 1;
  2103. else
  2104. {
  2105. if(!can_move_to(xCoord-1,yCoord+2) || !can_move_to(xCoord,yCoord+2))
  2106. prefer_upper_s2 = 1;
  2107. else
  2108. prefer_lower_s2 = 1;
  2109. }
  2110. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2111. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  2112. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2113. err_when(!(nodeType&0x2) && !can_move_to(xCoord, yCoord+2));
  2114. max_size_result_node_ptr->node_x = xCoord;
  2115. max_size_result_node_ptr->node_y = yCoord+(prefer_lower_s2 || !(nodeType&0x2));
  2116. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  2117. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2118. break;
  2119. case 2: // 2 points, 2 possible paths
  2120. if(parent_result_node_ptr->node_y<=yCoord+1)
  2121. prefer_upper_s2 = 1;
  2122. else
  2123. {
  2124. if(!can_move_to(xCoord-1, yCoord+3) || !can_move_to(xCoord, yCoord+3) ||
  2125. !can_move_to(xCoord+1, yCoord+3))
  2126. prefer_upper_s2 = 1;
  2127. else
  2128. prefer_lower_s2 = 1;
  2129. }
  2130. max_size_result_node_ptr->node_x = xCoord-1;
  2131. max_size_result_node_ptr->node_y = yCoord+1+prefer_lower_s2;
  2132. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2133. max_size_result_node_ptr->node_x = xCoord;
  2134. max_size_result_node_ptr->node_y = yCoord+1+prefer_lower_s2;
  2135. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2136. break;
  2137. case 3:
  2138. max_size_result_node_ptr->node_x = xCoord;
  2139. max_size_result_node_ptr->node_y = yCoord+1;
  2140. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2141. break;
  2142. case 5:
  2143. max_size_result_node_ptr->node_x = xCoord+1;
  2144. max_size_result_node_ptr->node_y = yCoord;
  2145. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2146. break;
  2147. case 6: // 2 points, 2 possible paths
  2148. if(parent_result_node_ptr->node_x<=xCoord+1)
  2149. prefer_left_s2 = 1;
  2150. else
  2151. {
  2152. if(!can_move_to(xCoord+3,yCoord-1) || !can_move_to(xCoord+3,yCoord) ||
  2153. !can_move_to(xCoord+3,yCoord+1))
  2154. prefer_left_s2 = 1;
  2155. else
  2156. prefer_right_s2 = 1;
  2157. }
  2158. max_size_result_node_ptr->node_x = xCoord+1+prefer_right_s2;
  2159. max_size_result_node_ptr->node_y = yCoord-1;
  2160. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2161. max_size_result_node_ptr->node_x = xCoord+1+prefer_right_s2;
  2162. max_size_result_node_ptr->node_y = yCoord;
  2163. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2164. break;
  2165. case 7: // 2 points, 2 paths
  2166. max_size_result_node_ptr->node_y = yCoord-1;
  2167. //----------- process path selection ---------------//
  2168. if(parent_result_node_ptr->node_x<=xCoord)
  2169. prefer_left_s2 = 1;
  2170. else
  2171. {
  2172. if(!can_move_to(xCoord+2,yCoord-1) || !can_move_to(xCoord+2,yCoord))
  2173. prefer_left_s2 = 1;
  2174. else
  2175. prefer_right_s2 = 1;
  2176. }
  2177. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2178. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2179. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2180. err_when(!(nodeType&0x4) && !can_move_to(xCoord+2, yCoord));
  2181. max_size_result_node_ptr->node_x = xCoord+(prefer_right_s2 || !(nodeType&0x4));
  2182. max_size_result_node_ptr->node_y = yCoord;
  2183. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2184. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2185. break;
  2186. case 8: // 2 points
  2187. max_size_result_node_ptr->node_x = xCoord-1;
  2188. max_size_result_node_ptr->node_y = yCoord-1;
  2189. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2190. err_when(nodeType!=15);
  2191. max_size_result_node_ptr->node_x = xCoord;
  2192. max_size_result_node_ptr->node_y = yCoord;
  2193. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2194. break;
  2195. default: err_here();
  2196. break;
  2197. }
  2198. break;
  2199. case 5: switch(exitDirection)
  2200. {
  2201. case 1: // 2 points, 2 paths
  2202. max_size_result_node_ptr->node_x = xCoord-1;
  2203. //----------- process path selection ---------------//
  2204. if(parent_result_node_ptr->node_y<=yCoord)
  2205. prefer_upper_s2 = 1;
  2206. else
  2207. {
  2208. if(!can_move_to(xCoord-1,yCoord+2) || !can_move_to(xCoord,yCoord+2) ||
  2209. !can_move_to(xCoord+1,yCoord+2))
  2210. prefer_upper_s2 = 1;
  2211. else
  2212. prefer_lower_s2 = 1;
  2213. }
  2214. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2215. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  2216. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2217. max_size_result_node_ptr->node_x = xCoord;
  2218. max_size_result_node_ptr->node_y = yCoord+prefer_lower_s2;
  2219. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  2220. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2221. break;
  2222. case 2: // 2 points, 2 paths
  2223. max_size_result_node_ptr->node_x = xCoord-1;
  2224. max_size_result_node_ptr->node_y = yCoord+1;
  2225. //----------- process path selection ---------------//
  2226. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2227. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2228. prefer_lower_s2 = 1;
  2229. else
  2230. prefer_upper_s2 = 1;
  2231. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2232. err_when(!(nodeType&0x1) && !can_move_to(xCoord+1, yCoord+2));
  2233. max_size_result_node_ptr->node_x = xCoord;
  2234. if(prefer_upper_s2)
  2235. max_size_result_node_ptr->node_y = yCoord + !(nodeType&0x1);
  2236. else // prefer_lower_s2
  2237. max_size_result_node_ptr->node_y = yCoord + can_move_to(xCoord+1, yCoord+2);
  2238. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord+1);
  2239. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2240. break;
  2241. case 3: // 2 possible paths
  2242. if(parent_result_node_ptr->node_x<=xCoord)
  2243. prefer_left_s2 = 1;
  2244. else
  2245. {
  2246. if(can_move_to(xCoord+2,yCoord+2))
  2247. prefer_right_s2 = 1;
  2248. else
  2249. prefer_left_s2 = 1;
  2250. }
  2251. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2252. max_size_result_node_ptr->node_y = yCoord+1;
  2253. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2254. break;
  2255. case 4: // 2 possible paths
  2256. if(parent_result_node_ptr->node_x>xCoord)
  2257. prefer_right_s2 = 1;
  2258. else
  2259. {
  2260. if(!can_move_to(xCoord,yCoord+1) || !can_move_to(xCoord,yCoord+2))
  2261. prefer_right_s2 = 1;
  2262. else
  2263. prefer_left_s2 = 1;
  2264. }
  2265. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2266. max_size_result_node_ptr->node_y = yCoord+1;
  2267. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2268. break;
  2269. case 6: // 2 possible paths
  2270. if(parent_result_node_ptr->node_x>xCoord)
  2271. prefer_right_s2 = 1;
  2272. else
  2273. {
  2274. if(!can_move_to(xCoord,yCoord-1) || !can_move_to(xCoord,yCoord))
  2275. prefer_right_s2 = 1;
  2276. else
  2277. prefer_left_s2 = 1;
  2278. }
  2279. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2280. max_size_result_node_ptr->node_y = yCoord-1;
  2281. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2282. break;
  2283. case 7: // 2 possible paths
  2284. if(parent_result_node_ptr->node_x<=xCoord)
  2285. prefer_left_s2 = 1;
  2286. else
  2287. {
  2288. if(can_move_to(xCoord+2,yCoord-1))
  2289. prefer_right_s2 = 1;
  2290. else
  2291. prefer_left_s2 = 1;
  2292. }
  2293. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2294. max_size_result_node_ptr->node_y = yCoord-1;
  2295. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2296. break;
  2297. case 8: // 2 points
  2298. max_size_result_node_ptr->node_x = xCoord-1;
  2299. max_size_result_node_ptr->node_y = yCoord-1;
  2300. //----------- process path selection ---------------//
  2301. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2302. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2303. prefer_upper_s2 = 1;
  2304. else
  2305. prefer_lower_s2 = 1;
  2306. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2307. err_when(!(nodeType&0x4) && !can_move_to(xCoord+1, yCoord-1));
  2308. max_size_result_node_ptr->node_x = xCoord;
  2309. if(prefer_upper_s2)
  2310. max_size_result_node_ptr->node_y = yCoord - can_move_to(xCoord+1, yCoord-1);
  2311. else // prefer_lower_s2
  2312. max_size_result_node_ptr->node_y = yCoord - !(nodeType&0x4);
  2313. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2314. break;
  2315. default: err_here();
  2316. break;
  2317. }
  2318. break;
  2319. case 6: switch(exitDirection)
  2320. {
  2321. case 1: // 2 points, 2 paths
  2322. max_size_result_node_ptr->node_x = xCoord-1;
  2323. max_size_result_node_ptr->node_y = yCoord;
  2324. //----------- process path selection ---------------//
  2325. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2326. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2327. prefer_lower_s2 = 1;
  2328. else
  2329. prefer_upper_s2 = 1;
  2330. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2331. err_when(!(nodeType&0x8) && !can_move_to(xCoord, yCoord-1));
  2332. max_size_result_node_ptr->node_x = xCoord;
  2333. if(prefer_upper_s2)
  2334. max_size_result_node_ptr->node_y = yCoord - can_move_to(xCoord, yCoord-1);
  2335. else // prefer_lower_s2
  2336. max_size_result_node_ptr->node_y = yCoord - !(nodeType&0x8);
  2337. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord-1);
  2338. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2339. break;
  2340. case 2: // 2 points
  2341. max_size_result_node_ptr->node_x = xCoord-1;
  2342. max_size_result_node_ptr->node_y = yCoord+1;
  2343. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2344. err_when(nodeType != 15);
  2345. max_size_result_node_ptr->node_x = xCoord;
  2346. max_size_result_node_ptr->node_y = yCoord;
  2347. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2348. break;
  2349. case 3: // 2 points, 2 paths
  2350. max_size_result_node_ptr->node_x = xCoord;
  2351. max_size_result_node_ptr->node_y = yCoord+1;
  2352. //----------- process path selection ---------------//
  2353. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2354. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2355. prefer_left_s2 = 1;
  2356. else
  2357. prefer_right_s2 = 1;
  2358. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2359. err_when(!(nodeType&0x1) && !can_move_to(xCoord+2, yCoord+1));
  2360. max_size_result_node_ptr->node_y = yCoord;
  2361. if(prefer_left_s2)
  2362. max_size_result_node_ptr->node_x = xCoord + !(nodeType&0x1);
  2363. else // prefer_right_s2
  2364. max_size_result_node_ptr->node_x = xCoord + can_move_to(xCoord+2, yCoord+1);
  2365. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord-1);
  2366. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2367. break;
  2368. case 4: // 2 points, 2 possible paths
  2369. if(parent_result_node_ptr->node_x<=xCoord+1)
  2370. prefer_left_s2 = 1;
  2371. else
  2372. {
  2373. if(!can_move_to(xCoord+3,yCoord) || !can_move_to(xCoord+3,yCoord+1) ||
  2374. !can_move_to(xCoord+3,yCoord+2))
  2375. prefer_left_s2 = 1;
  2376. else
  2377. prefer_right_s2 = 1;
  2378. }
  2379. max_size_result_node_ptr->node_x = xCoord+1+prefer_right_s2;
  2380. max_size_result_node_ptr->node_y = yCoord+1;
  2381. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2382. max_size_result_node_ptr->node_x = xCoord+1+prefer_right_s2;
  2383. max_size_result_node_ptr->node_y = yCoord;
  2384. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2385. break;
  2386. case 5:
  2387. max_size_result_node_ptr->node_x = xCoord+1;
  2388. max_size_result_node_ptr->node_y = yCoord;
  2389. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2390. break;
  2391. case 7:
  2392. max_size_result_node_ptr->node_x = xCoord;
  2393. max_size_result_node_ptr->node_y = yCoord-1;
  2394. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2395. break;
  2396. case 8: // 2 points, 2 possible paths
  2397. if(parent_result_node_ptr->node_y>=yCoord-1)
  2398. prefer_lower_s2 = 1;
  2399. else
  2400. {
  2401. if(!can_move_to(xCoord-1,yCoord-2) || !can_move_to(xCoord,yCoord-2) ||
  2402. !can_move_to(xCoord+1,yCoord-2)) // must check for all 3 points to avoid problems in dummy node generated in path-reuse
  2403. prefer_lower_s2 = 1;
  2404. else
  2405. prefer_upper_s2 = 1;
  2406. }
  2407. max_size_result_node_ptr->node_x = xCoord-1;
  2408. max_size_result_node_ptr->node_y = yCoord-1-prefer_upper_s2;
  2409. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2410. max_size_result_node_ptr->node_x = xCoord;
  2411. max_size_result_node_ptr->node_y = yCoord-1-prefer_upper_s2;
  2412. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2413. break;
  2414. default: err_here();
  2415. break;
  2416. }
  2417. break;
  2418. case 7: switch(exitDirection)
  2419. {
  2420. case 1: // 2 possible paths
  2421. if(parent_result_node_ptr->node_y>=yCoord)
  2422. prefer_lower_s2 = 1;
  2423. else
  2424. {
  2425. if(can_move_to(xCoord-1,yCoord-1))
  2426. prefer_upper_s2 = 1;
  2427. else
  2428. prefer_lower_s2 = 1;
  2429. }
  2430. max_size_result_node_ptr->node_x = xCoord-1;
  2431. max_size_result_node_ptr->node_y = yCoord-prefer_upper_s2;
  2432. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2433. break;
  2434. case 2: // 2 points, 2 paths
  2435. max_size_result_node_ptr->node_x = xCoord-1;
  2436. max_size_result_node_ptr->node_y = yCoord+1;
  2437. //----------- process path selection ---------------//
  2438. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2439. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2440. prefer_left_s2 = 1;
  2441. else
  2442. prefer_right_s2 = 1;
  2443. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2444. err_when(!(nodeType&0x8) && !can_move_to(xCoord-1, yCoord));
  2445. max_size_result_node_ptr->node_y = yCoord;
  2446. if(prefer_left_s2)
  2447. max_size_result_node_ptr->node_x = xCoord - can_move_to(xCoord-1, yCoord);
  2448. else // prefer_right_s2
  2449. max_size_result_node_ptr->node_x = xCoord - !(nodeType&0x8);
  2450. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord-1);
  2451. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2452. break;
  2453. case 3: // 2 points, 2 paths
  2454. max_size_result_node_ptr->node_y = yCoord+1;
  2455. //----------- process path selection ---------------//
  2456. if(parent_result_node_ptr->node_x<=xCoord)
  2457. prefer_left_s2 = 1;
  2458. else
  2459. {
  2460. if(!can_move_to(xCoord+2,yCoord) || !can_move_to(xCoord+2,yCoord+1) ||
  2461. !can_move_to(xCoord+2,yCoord+2))
  2462. prefer_left_s2 = 1;
  2463. else
  2464. prefer_right_s2 = 1;
  2465. }
  2466. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2467. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2468. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2469. max_size_result_node_ptr->node_x = xCoord+prefer_right_s2;
  2470. max_size_result_node_ptr->node_y = yCoord;
  2471. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2472. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2473. break;
  2474. case 4: // 2 points, 2 paths
  2475. max_size_result_node_ptr->node_x = xCoord+1;
  2476. max_size_result_node_ptr->node_y = yCoord+1;
  2477. //----------- process path selection ---------------//
  2478. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2479. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2480. prefer_right_s2 = 1;
  2481. else
  2482. prefer_left_s2 = 1;
  2483. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2484. err_when(!(nodeType&0x4) && !can_move_to(xCoord+2, yCoord));
  2485. max_size_result_node_ptr->node_y = yCoord;
  2486. if(prefer_left_s2)
  2487. max_size_result_node_ptr->node_x = xCoord + !(nodeType&0x4);
  2488. else // prefer_right_s2
  2489. max_size_result_node_ptr->node_x = xCoord + can_move_to(xCoord+2, yCoord);
  2490. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord+1);
  2491. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2492. break;
  2493. case 5: // 2 possible paths
  2494. if(parent_result_node_ptr->node_y>=yCoord)
  2495. prefer_lower_s2 = 1;
  2496. else
  2497. {
  2498. if(can_move_to(xCoord+1,yCoord-1))
  2499. prefer_upper_s2 = 1;
  2500. else
  2501. prefer_lower_s2 = 1;
  2502. }
  2503. max_size_result_node_ptr->node_x = xCoord+1;
  2504. max_size_result_node_ptr->node_y = yCoord-prefer_upper_s2;
  2505. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2506. break;
  2507. case 6: // 2 possible paths
  2508. if(parent_result_node_ptr->node_y<yCoord)
  2509. prefer_upper_s2 = 1;
  2510. else
  2511. {
  2512. if(!can_move_to(xCoord+1,yCoord+1) || !can_move_to(xCoord+2,yCoord+1))
  2513. prefer_upper_s2 = 1;
  2514. else
  2515. prefer_lower_s2 = 1;
  2516. }
  2517. max_size_result_node_ptr->node_x = xCoord+1;
  2518. max_size_result_node_ptr->node_y = yCoord-prefer_upper_s2;
  2519. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2520. break;
  2521. case 8: // 2 possible paths
  2522. if(parent_result_node_ptr->node_y<yCoord)
  2523. prefer_upper_s2 = 1;
  2524. else
  2525. {
  2526. if(!can_move_to(xCoord-1,yCoord+1) || !can_move_to(xCoord,yCoord+1))
  2527. prefer_lower_s2 = 1;
  2528. else
  2529. prefer_upper_s2 = 1;
  2530. }
  2531. max_size_result_node_ptr->node_x = xCoord-1;
  2532. max_size_result_node_ptr->node_y = yCoord-prefer_upper_s2;
  2533. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2534. break;
  2535. default: err_here();
  2536. break;
  2537. }
  2538. break;
  2539. case 8: switch(exitDirection)
  2540. {
  2541. case 1:
  2542. max_size_result_node_ptr->node_x = xCoord-1;
  2543. max_size_result_node_ptr->node_y = yCoord;
  2544. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2545. break;
  2546. case 2: // 2 points, 2 possible paths
  2547. if(parent_result_node_ptr->node_x>=xCoord-1)
  2548. prefer_right_s2 = 1;
  2549. else
  2550. {
  2551. if(!can_move_to(xCoord-2,yCoord) || !can_move_to(xCoord-2,yCoord+1) ||
  2552. !can_move_to(xCoord-2,yCoord+2))
  2553. prefer_right_s2 = 1;
  2554. else
  2555. prefer_left_s2 = 1;
  2556. }
  2557. max_size_result_node_ptr->node_x = xCoord-1-prefer_left_s2;
  2558. max_size_result_node_ptr->node_y = yCoord+1;
  2559. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2560. max_size_result_node_ptr->node_x = xCoord-1-prefer_left_s2;
  2561. max_size_result_node_ptr->node_y = yCoord;
  2562. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2563. break;
  2564. case 3: // 2 points, 2 paths
  2565. max_size_result_node_ptr->node_x = xCoord;
  2566. max_size_result_node_ptr->node_y = yCoord+1;
  2567. //----------- process path selection ---------------//
  2568. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2569. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2570. prefer_right_s2 = 1;
  2571. else
  2572. prefer_left_s2 = 1;
  2573. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2574. err_when(!(nodeType&0x2) && !can_move_to(xCoord-1, yCoord+1));
  2575. max_size_result_node_ptr->node_y = yCoord;
  2576. if(prefer_left_s2)
  2577. max_size_result_node_ptr->node_x = xCoord - can_move_to(xCoord-1, yCoord+1);
  2578. else // prefer_right_s2
  2579. max_size_result_node_ptr->node_x = xCoord - !(nodeType&0x2);
  2580. err_when(max_size_result_node_ptr->node_x!=xCoord && max_size_result_node_ptr->node_x!=xCoord-1);
  2581. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2582. break;
  2583. case 4: // 2 points
  2584. max_size_result_node_ptr->node_x = xCoord+1;
  2585. max_size_result_node_ptr->node_y = yCoord+1;
  2586. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2587. err_when(nodeType!=15);
  2588. max_size_result_node_ptr->node_x = xCoord;
  2589. max_size_result_node_ptr->node_y = yCoord;
  2590. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2591. break;
  2592. case 5: // 2 points, 2 paths
  2593. max_size_result_node_ptr->node_x = xCoord+1;
  2594. max_size_result_node_ptr->node_y = yCoord;
  2595. //----------- process path selection ---------------//
  2596. if( (parent_result_node_ptr->node_x-max_size_result_node_ptr->node_x==0) ||
  2597. (parent_result_node_ptr->node_y-max_size_result_node_ptr->node_y==0) )
  2598. prefer_lower_s2 = 1;
  2599. else
  2600. prefer_upper_s2 = 1;
  2601. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2602. err_when(!(nodeType&0x4) && !can_move_to(xCoord+1, yCoord-1));
  2603. max_size_result_node_ptr->node_x = xCoord;
  2604. if(prefer_upper_s2)
  2605. max_size_result_node_ptr->node_y = yCoord - can_move_to(xCoord+1, yCoord-1);
  2606. else // prefer_lower_s2
  2607. max_size_result_node_ptr->node_y = yCoord - !(nodeType&0x4);
  2608. err_when(max_size_result_node_ptr->node_y!=yCoord && max_size_result_node_ptr->node_y!=yCoord-1);
  2609. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2610. break;
  2611. case 6: // 2 points, 2 possible paths
  2612. if(parent_result_node_ptr->node_y>=yCoord-1)
  2613. prefer_lower_s2 = 1;
  2614. else
  2615. {
  2616. if(!can_move_to(xCoord,yCoord-2) || !can_move_to(xCoord+1,yCoord-2) ||
  2617. !can_move_to(xCoord+2,yCoord-2))
  2618. prefer_lower_s2 = 1;
  2619. else
  2620. prefer_upper_s2 = 1;
  2621. }
  2622. max_size_result_node_ptr->node_x = xCoord+1;
  2623. max_size_result_node_ptr->node_y = yCoord-1-prefer_upper_s2;
  2624. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2625. max_size_result_node_ptr->node_x = xCoord;
  2626. max_size_result_node_ptr->node_y = yCoord-1-prefer_upper_s2;
  2627. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2628. break;
  2629. case 7:
  2630. max_size_result_node_ptr->node_x = xCoord;
  2631. max_size_result_node_ptr->node_y = yCoord-1;
  2632. add_result_node(&max_size_result_node_ptr, &parent_result_node_ptr, count);
  2633. break;
  2634. default: err_here();
  2635. break;
  2636. }
  2637. break;
  2638. default: err_here();
  2639. break;
  2640. }
  2641. }
  2642. //------ End of function SeekPathS2::get_real_result_node ---------//
  2643. //-------- Begin of function SeekPathS2::return_best_node -------//
  2644. //
  2645. // return : <NodeS2*> the best node.
  2646. // NULL if no more node on the open node list. There is no
  2647. // possible path between the starting and destination points.
  2648. //
  2649. NodeS2* SeekPathS2::return_best_node()
  2650. {
  2651. NodeS2 *tempNode;
  2652. if( !open_node_list )
  2653. return NULL;
  2654. //------------------------------------------------------------------//
  2655. // Pick Node with lowest f, in this case it's the first node in list
  2656. // Because we sort the open_node_list list wrt lowest f. Call it BESTNode.
  2657. //------------------------------------------------------------------//
  2658. tempNode=open_node_list; // point to first node on open_node_list
  2659. open_node_list=tempNode->next_node; // Make open_node_list point to nextnode or NULL.
  2660. //------------------------------------------------------------------//
  2661. // Next take BESTNode (or temp in this case) and put it on closed_node_list
  2662. //------------------------------------------------------------------//
  2663. tempNode->next_node=closed_node_list;
  2664. closed_node_list=tempNode;
  2665. return tempNode;
  2666. }
  2667. //-------- End of function SeekPathS2::return_best_node ---------//
  2668. //----- Begin of function SeekPathS2::return_closest_node -------//
  2669. //
  2670. // Return the node that is closest to the destination.
  2671. //
  2672. NodeS2* SeekPathS2::return_closest_node()
  2673. {
  2674. NodeS2* nodePtr;
  2675. NodeS2* shortestNode=NULL;
  2676. int nodeDistance, shortestDistance=0x7FFF;
  2677. int disX, disY;
  2678. //------------ search open_node_list ----------//
  2679. nodePtr = open_node_list;
  2680. int yieldCount = 0;
  2681. while(nodePtr)
  2682. {
  2683. //--- the distance between this node and the destination node ----//
  2684. nodeDistance = (disX=cur_dest_x_s2-nodePtr->node_x)*disX + (disY=cur_dest_y_s2-nodePtr->node_y)*disY;
  2685. //--- if the distance is shorter than the current shortest distance ---//
  2686. if( nodeDistance < shortestDistance )
  2687. {
  2688. shortestDistance = nodeDistance;
  2689. shortestNode = nodePtr;
  2690. }
  2691. nodePtr=nodePtr->next_node;
  2692. yieldCount++;
  2693. if(yieldCount%25==0)
  2694. sys_yield();
  2695. }
  2696. //------------ search closed_node_list ----------//
  2697. nodePtr = closed_node_list;
  2698. yieldCount = 0;
  2699. while(nodePtr)
  2700. {
  2701. //--- the distance between this node and the destination node ----//
  2702. nodeDistance = (disX=cur_dest_x_s2-nodePtr->node_x)*disX + (disY=cur_dest_y_s2-nodePtr->node_y)*disY;
  2703. //--- if the distance is shorter than the current shortest distance ---//
  2704. if( nodeDistance < shortestDistance )
  2705. {
  2706. shortestDistance = nodeDistance;
  2707. shortestNode = nodePtr;
  2708. }
  2709. nodePtr=nodePtr->next_node;
  2710. yieldCount++;
  2711. if(yieldCount%25==0)
  2712. sys_yield();
  2713. }
  2714. //------------------------------------------------------//
  2715. // there may be some nodes with the same shortest Distance
  2716. // from the destination node. However, one should be closer
  2717. // to the starting node. The following is used to find
  2718. // out the closer node.
  2719. //------------------------------------------------------//
  2720. nodePtr = shortestNode->parent_node;
  2721. yieldCount = 0;
  2722. while(nodePtr)
  2723. {
  2724. //--- the distance between the parent node and the destination node ----//
  2725. nodeDistance = (disX=cur_dest_x_s2-nodePtr->node_x)*disX + (disY=cur_dest_y_s2-nodePtr->node_y)*disY;
  2726. //--- if the distance is shorter than the current shortest distance ---//
  2727. if( nodeDistance <= shortestDistance )
  2728. {
  2729. shortestDistance = nodeDistance;
  2730. shortestNode = nodePtr;
  2731. }
  2732. else
  2733. break;
  2734. nodePtr=nodePtr->parent_node;
  2735. yieldCount++;
  2736. if(yieldCount%25==0)
  2737. sys_yield();
  2738. }
  2739. return shortestNode;
  2740. }
  2741. //----- End of function SeekPathS2::return_closest_node -------//
  2742. //----- Begin of function SeekPathS2::insert_open_node -------//
  2743. void SeekPathS2::insert_open_node(NodeS2 *succNode)
  2744. {
  2745. if (open_node_list == NULL)
  2746. {
  2747. open_node_list=succNode;
  2748. return;
  2749. }
  2750. //---- insert into open_node_list successor wrt f ----//
  2751. register int f=succNode->node_f;
  2752. if( open_node_list && open_node_list->node_f < f )
  2753. {
  2754. NodeS2 *tempNode1, *tempNode2;
  2755. tempNode1=open_node_list;
  2756. tempNode2=tempNode1->next_node;
  2757. int yieldCount = 0;
  2758. while( tempNode2 && tempNode2->node_f < f )
  2759. {
  2760. tempNode1=tempNode2;
  2761. tempNode2=tempNode2->next_node;
  2762. yieldCount++;
  2763. if(yieldCount%200==0)
  2764. sys_yield();
  2765. }
  2766. err_when(succNode!=NULL && tempNode2!=NULL && succNode->node_x==tempNode2->node_x &&
  2767. succNode->node_y==tempNode2->node_y); // avoid pointer pointing back to itself
  2768. succNode->next_node = tempNode2;
  2769. tempNode1->next_node = succNode;
  2770. }
  2771. else
  2772. {
  2773. succNode->next_node = open_node_list;
  2774. open_node_list = succNode;
  2775. }
  2776. }
  2777. //----- End of function SeekPathS2::insert_open_node -------//
  2778. //-------- Begin of function SeekPath::smooth_the_path ---------//
  2779. ResultNode* SeekPathS2::smooth_the_path(ResultNode* nodeArray, int& nodeCount)
  2780. {
  2781. //--------------------------------------------------//
  2782. // to remove duplicate or useless node
  2783. //--------------------------------------------------//
  2784. int i, j, curNodeCount = 1;
  2785. parent_result_node_ptr = nodeArray;
  2786. max_size_result_node_ptr = nodeArray+1;
  2787. for(i=0; i<nodeCount-1; i++)
  2788. {
  2789. if(parent_result_node_ptr->node_x!=max_size_result_node_ptr->node_x ||
  2790. parent_result_node_ptr->node_y!=max_size_result_node_ptr->node_y)
  2791. {
  2792. parent_result_node_ptr++;
  2793. parent_result_node_ptr->node_x = max_size_result_node_ptr->node_x;
  2794. parent_result_node_ptr->node_y = max_size_result_node_ptr->node_y;
  2795. curNodeCount++;
  2796. }
  2797. max_size_result_node_ptr++;
  2798. if(i%50==0)
  2799. sys_yield();
  2800. }
  2801. nodeCount = curNodeCount;
  2802. //return nodeArray;
  2803. //---------- declare variables ---------------//
  2804. int checkedNodeCount;
  2805. short vectorX, vectorY, newVectorX, newVectorY, newVectorX2, newVectorY2;
  2806. short changed, caseNum, processed;
  2807. ResultNode *curUsefulNodePtr = nodeArray+1;
  2808. ResultNode *ptr1;
  2809. ResultNode *ptr2;
  2810. ResultNode *ptr3;
  2811. ResultNode *ptr4;
  2812. //--------------------------------------------------//
  2813. // smooth the path
  2814. //--------------------------------------------------//
  2815. parent_result_node_ptr = nodeArray;
  2816. max_size_result_node_ptr = nodeArray+1;
  2817. changed = 1;
  2818. checkedNodeCount = 1;
  2819. curNodeCount = nodeCount;
  2820. ptr1 = parent_result_node_ptr;
  2821. ptr2 = max_size_result_node_ptr;
  2822. ptr3 = max_size_result_node_ptr+1;
  2823. int yieldCount = 1;
  2824. while(changed && curNodeCount>=3)
  2825. {
  2826. yieldCount++;
  2827. if(yieldCount%20==0)
  2828. sys_yield();
  2829. vectorX = ptr2->node_x - ptr1->node_x;
  2830. vectorY = ptr2->node_y - ptr1->node_y;
  2831. changed = 0;
  2832. for(j=1; j<curNodeCount-1; j++)
  2833. {
  2834. processed = 0;
  2835. newVectorX= ptr3->node_x - ptr2->node_x;
  2836. newVectorY= ptr3->node_y - ptr2->node_y;
  2837. //----------------------------------------------------------//
  2838. // reverse direction
  2839. //----------------------------------------------------------//
  2840. if(vectorX==-newVectorX && vectorY==-newVectorY)
  2841. {
  2842. if(j+2<curNodeCount) //------ not the end of the array ------//
  2843. {
  2844. ptr2 = ptr3+1;
  2845. ptr3 = ptr2+1;
  2846. vectorX = ptr2->node_x - ptr1->node_x;
  2847. vectorY = ptr2->node_y - ptr1->node_y;
  2848. processed = 1;
  2849. j++;
  2850. continue;
  2851. }
  2852. else //-------- already the end of the array ----------//
  2853. {
  2854. ptr3++;
  2855. changed++;
  2856. break; // break the for loop
  2857. }
  2858. }
  2859. //----------------------------------------------------------//
  2860. // turning 45 degree angle
  2861. //----------------------------------------------------------//
  2862. if(abs(vectorX+newVectorX)<=1 && abs(vectorY+newVectorY)<=1 && // if so, turning 45 or 90 degree
  2863. vectorX*newVectorX+vectorY*newVectorY!=0) // reject turning 90 degree
  2864. {
  2865. ptr2=ptr3;
  2866. ptr3++;
  2867. vectorX = ptr2->node_x - ptr1->node_x;
  2868. vectorY = ptr2->node_y - ptr1->node_y;
  2869. processed = 1;
  2870. continue;
  2871. }
  2872. //----------------------------------------------------------//
  2873. // turning at 90 degree clockwise / anti-clockwise
  2874. //----------------------------------------------------------//
  2875. if((vectorX==0 && vectorY!=0 && newVectorX!=0 && newVectorY==0) || // + case
  2876. (vectorX!=0 && vectorY==0 && newVectorX==0 && newVectorY!=0))
  2877. {
  2878. ptr2=ptr3;
  2879. ptr3++;
  2880. vectorX = ptr2->node_x - ptr1->node_x;
  2881. vectorY = ptr2->node_y - ptr1->node_y;
  2882. processed = 1;
  2883. continue;
  2884. }
  2885. if((vectorX!=0 && vectorY!=0 && newVectorX!=0 && newVectorY!=0) && // x case
  2886. ((vectorX==newVectorX && vectorY==-newVectorY) || (vectorX==-newVectorX && vectorY==newVectorY)))
  2887. {
  2888. ptr2->node_x = (ptr1->node_x+ptr3->node_x)/2;
  2889. ptr2->node_y = (ptr1->node_y+ptr3->node_y)/2;
  2890. curUsefulNodePtr->node_x = ptr2->node_x;
  2891. curUsefulNodePtr->node_y = ptr2->node_y;
  2892. curUsefulNodePtr++;
  2893. checkedNodeCount++;
  2894. ptr1 = ptr2;
  2895. ptr2 = ptr3;
  2896. ptr3++;
  2897. vectorX = ptr2->node_x - ptr1->node_x;
  2898. vectorY = ptr2->node_y - ptr1->node_y;
  2899. processed = 1;
  2900. continue;
  2901. }
  2902. //----------------------------------------------------------//
  2903. // check for the 4-point case
  2904. //----------------------------------------------------------//
  2905. if(j<curNodeCount-2)
  2906. {
  2907. ptr4 = ptr3+1;
  2908. newVectorX2 = ptr4->node_x - ptr3->node_x;
  2909. newVectorY2 = ptr4->node_y - ptr3->node_y;
  2910. caseNum = 0;
  2911. if(!caseNum && vectorX==-1 && vectorY==0 && newVectorX==-1 && newVectorX2==0)
  2912. {
  2913. if(newVectorY==1 && newVectorY2==1 && can_move_to(ptr1->node_x, ptr4->node_y))
  2914. caseNum = 1;
  2915. if(!caseNum && newVectorY==-1 && newVectorY2==-1 && can_move_to(ptr1->node_x, ptr4->node_y-1))
  2916. caseNum = 5;
  2917. }
  2918. if(!caseNum && vectorX==1 && vectorY==0 && newVectorX==1 && newVectorX2==0)
  2919. {
  2920. if(newVectorY==1 && newVectorY2==1 && can_move_to(ptr1->node_x+1, ptr4->node_y))
  2921. caseNum = 3;
  2922. if(!caseNum && newVectorY==-1 && newVectorY2==-1 && can_move_to(ptr1->node_x+1, ptr4->node_y-1))
  2923. caseNum = 7;
  2924. }
  2925. if(!caseNum && vectorX==0 && vectorY==-1 && newVectorY==-1 && newVectorY2==0)
  2926. {
  2927. if(newVectorX==1 && newVectorX2==1 && can_move_to(ptr4->node_x, ptr1->node_y))
  2928. caseNum = 2;
  2929. if(!caseNum && newVectorX==-1 && newVectorX2==-1 && can_move_to(ptr4->node_x+1, ptr1->node_y))
  2930. caseNum = 4;
  2931. }
  2932. if(!caseNum && vectorX==0 && vectorY==1 && newVectorY==1 && newVectorY2==0)
  2933. {
  2934. if(newVectorX==1 && newVectorX2==1 && can_move_to(ptr4->node_x, ptr1->node_y+1))
  2935. caseNum = 6;
  2936. if(!caseNum && newVectorX==-1 && newVectorX2==-1 && can_move_to(ptr4->node_x+1, ptr1->node_y+1))
  2937. caseNum = 8;
  2938. }
  2939. if(caseNum)
  2940. {
  2941. switch(caseNum)
  2942. {
  2943. case 1:
  2944. if(can_move_to(ptr1->node_x, ptr4->node_y))
  2945. {
  2946. //ptr2->node_x = ptr2->node_x;
  2947. ptr2->node_y++; //ptr2->node_y = ptr3->node_y;
  2948. processed = 1;
  2949. }
  2950. break;
  2951. case 2:
  2952. if(can_move_to(ptr4->node_x, ptr1->node_y))
  2953. {
  2954. ptr2->node_x++; //ptr2->node_x = ptr3->node_x;
  2955. //ptr2->node_y = ptr2->node_y;
  2956. processed = 1;
  2957. }
  2958. break;
  2959. case 3:
  2960. if(can_move_to(ptr2->node_x, ptr4->node_y))
  2961. {
  2962. //ptr2->node_x = ptr2->node_x;
  2963. ptr2->node_y++; //ptr2->node_y = ptr3->node_y;
  2964. processed = 1;
  2965. }
  2966. break;
  2967. case 4:
  2968. if(can_move_to(ptr3->node_x, ptr1->node_y))
  2969. {
  2970. ptr2->node_x--; //ptr2->node_x = ptr3->node_x;
  2971. //ptr2->node_y = ptr2->node_y;
  2972. processed = 1;
  2973. }
  2974. break;
  2975. case 5:
  2976. if(can_move_to(ptr1->node_x, ptr3->node_y))
  2977. {
  2978. //ptr2->node_x = ptr2->node_x;
  2979. ptr2->node_y--; //ptr2->node_y = ptr3->node_y;
  2980. processed = 1;
  2981. }
  2982. break;
  2983. case 6:
  2984. if(can_move_to(ptr4->node_x, ptr2->node_y))
  2985. {
  2986. ptr2->node_x++; //ptr2->node_x = ptr3->node_x;
  2987. //ptr2->node_y = ptr2->node_y;
  2988. processed = 1;
  2989. }
  2990. break;
  2991. case 7:
  2992. if(can_move_to(ptr2->node_x, ptr3->node_y))
  2993. {
  2994. //ptr2->node_x = ptr2->node_x;
  2995. ptr2->node_y--; //ptr2->node_y = ptr3->node_y;
  2996. processed = 1;
  2997. }
  2998. break;
  2999. case 8:
  3000. if(can_move_to(ptr3->node_x, ptr2->node_y))
  3001. {
  3002. ptr2->node_x--; //ptr2->node_x = ptr3->node_x;
  3003. //ptr2->node_y = ptr2->node_y;
  3004. processed = 1;
  3005. }
  3006. break;
  3007. default:
  3008. err_here();
  3009. break;
  3010. }
  3011. if(processed)
  3012. {
  3013. ptr3 = ptr4;
  3014. curUsefulNodePtr->node_x = ptr2->node_x;
  3015. curUsefulNodePtr->node_y = ptr2->node_y;
  3016. curUsefulNodePtr++;
  3017. checkedNodeCount++;
  3018. ptr1 = ptr2;
  3019. ptr2 = ptr3;
  3020. ptr3++;
  3021. j++;
  3022. vectorX = ptr2->node_x - ptr1->node_x;
  3023. vectorY = ptr2->node_y - ptr1->node_y;
  3024. continue;
  3025. }
  3026. }
  3027. }
  3028. //------ none of the above case ---------//
  3029. if(!processed)
  3030. {
  3031. curUsefulNodePtr->node_x = ptr2->node_x;
  3032. curUsefulNodePtr->node_y = ptr2->node_y;
  3033. curUsefulNodePtr++;
  3034. checkedNodeCount++;
  3035. }
  3036. else
  3037. changed = 1;
  3038. ptr1 = ptr2;
  3039. ptr2 = ptr3;
  3040. ptr3++;
  3041. vectorX = ptr2->node_x - ptr1->node_x;
  3042. vectorY = ptr2->node_y - ptr1->node_y;
  3043. }
  3044. //---- end checking and then reset parameters----//
  3045. curUsefulNodePtr->node_x = ptr2->node_x;
  3046. curUsefulNodePtr->node_y = ptr2->node_y;
  3047. curUsefulNodePtr++;
  3048. checkedNodeCount++;
  3049. curNodeCount = checkedNodeCount;
  3050. checkedNodeCount = 1;
  3051. ptr1 = parent_result_node_ptr;
  3052. ptr2 = ptr1+1;
  3053. ptr3 = ptr2+1;
  3054. }
  3055. nodeCount = curNodeCount;
  3056. return nodeArray;
  3057. }
  3058. //------- End of function SeekPathS2::smooth_the_path -------//
  3059. //-------- Begin of function NodeS2::generate_successors ---------//
  3060. // Note: In fact, the cost of the starting node should be 0 or 1
  3061. // and the parentEnterDirection is 0. Now the cost in this
  3062. // case is set to 2. The difference can be ignored as it will
  3063. // not affect the search after generating the second level
  3064. // children.
  3065. // The advantage to ignore this case is that less comparsion
  3066. // effort in checking parentEnterDirection.
  3067. //
  3068. short NodeS2::generate_successors(short parentEnterDirection)
  3069. {
  3070. int hasLeft = node_x > cur_border_x1_s2;
  3071. int hasRight = node_x < cur_border_x2_s2;
  3072. int hasUp = node_y > cur_border_y1_s2;
  3073. int hasDown = node_y < cur_border_y2_s2;
  3074. int upperLeftX,upperLeftY;
  3075. short cost;
  3076. upperLeftX = node_x<<1;
  3077. upperLeftY = node_y<<1;
  3078. err_when(upperLeftX != node_x+node_x || upperLeftY != node_y+node_y);
  3079. //-------------------------------------------
  3080. // enter_direction = (exit_direction+3)%8+1
  3081. //-------------------------------------------
  3082. if( hasLeft )
  3083. {
  3084. //--------- Left, exit_direction=1 --------//
  3085. if(can_move_to(upperLeftX-1, upperLeftY) && node_type&0x1 &&
  3086. can_move_to(upperLeftX-1, upperLeftY+1) && node_type&0x4 )
  3087. {
  3088. if(parentEnterDirection==2 || parentEnterDirection==3 || parentEnterDirection==7 || parentEnterDirection==8)
  3089. cost = 1;
  3090. else
  3091. {
  3092. if((parentEnterDirection==4 && (node_type&0x2 || can_move_to(upperLeftX,upperLeftY+2))) ||
  3093. parentEnterDirection==5 ||
  3094. (parentEnterDirection==6 && (node_type&0x8 || can_move_to(upperLeftX,upperLeftY-1))))
  3095. cost = 2;
  3096. else
  3097. {
  3098. if(parentEnterDirection==0 && !source_blocked_exit_direction[0]) // direction 1
  3099. {
  3100. if(source_locate_type==1 || source_locate_type==3)
  3101. cost = 1;
  3102. else
  3103. cost = 2;
  3104. }
  3105. else
  3106. cost = 0;
  3107. }
  3108. }
  3109. if(cost)
  3110. {
  3111. err_when(parentEnterDirection==1);
  3112. if(generate_succ(node_x-1,node_y,5,cost))
  3113. return 1;
  3114. }
  3115. }
  3116. if( hasUp )
  3117. {
  3118. //------- Upper-Left, exit_direction=8 ---------//
  3119. if(can_move_to(upperLeftX-1, upperLeftY-1) && can_move_to(upperLeftX, upperLeftY-1) &&
  3120. can_move_to(upperLeftX-1, upperLeftY) && node_type&0x1)
  3121. {
  3122. if(parentEnterDirection==1 || parentEnterDirection==2 || parentEnterDirection==6 || parentEnterDirection==7)
  3123. cost = 1;
  3124. else
  3125. {
  3126. if((parentEnterDirection==3 && (node_type&0x2 || can_move_to(upperLeftX-1, upperLeftY+1))) ||
  3127. (parentEnterDirection==4 && node_type==15) ||
  3128. (parentEnterDirection==5 && (node_type&0x4 || can_move_to(upperLeftX+1, upperLeftY-1))))
  3129. cost = 2;
  3130. else
  3131. {
  3132. if(parentEnterDirection==0 && !source_blocked_exit_direction[7]) // direction 8
  3133. {
  3134. if(source_locate_type==1)
  3135. cost = 1;
  3136. else
  3137. cost = 2;
  3138. }
  3139. else
  3140. cost = 0;
  3141. }
  3142. }
  3143. if(cost)
  3144. {
  3145. err_when(parentEnterDirection==8);
  3146. if(generate_succ(node_x-1,node_y-1,4,cost))
  3147. return 1;
  3148. }
  3149. }
  3150. }
  3151. if( hasDown )
  3152. {
  3153. //--------- Lower-Left, exit_direction=2 ----------//
  3154. if(can_move_to(upperLeftX-1,upperLeftY+1) && node_type&0x4 &&
  3155. can_move_to(upperLeftX-1,upperLeftY+2) && can_move_to(upperLeftX,upperLeftY+2))
  3156. {
  3157. if(parentEnterDirection==1 || parentEnterDirection==3 || parentEnterDirection==4 || parentEnterDirection==8)
  3158. cost = 1;
  3159. else
  3160. {
  3161. if((parentEnterDirection==5 && (node_type&0x1 || can_move_to(upperLeftX+1,upperLeftY+2))) ||
  3162. (parentEnterDirection==6 && node_type==15) ||
  3163. (parentEnterDirection==7 && (node_type&0x8 || can_move_to(upperLeftX-1,upperLeftY))))
  3164. cost = 2;
  3165. else
  3166. {
  3167. if(parentEnterDirection==0 && !source_blocked_exit_direction[1]) // direction 2
  3168. {
  3169. if(source_locate_type==1||source_locate_type==3)
  3170. cost = 1;
  3171. else
  3172. cost = 2;
  3173. }
  3174. else
  3175. cost = 0;
  3176. }
  3177. }
  3178. if(cost || (parentEnterDirection==0 && source_locate_type==3 && !source_blocked_exit_direction[1]))
  3179. {
  3180. err_when(parentEnterDirection==2);
  3181. if(generate_succ(node_x-1,node_y+1,6,cost))
  3182. return 1;
  3183. }
  3184. }
  3185. }
  3186. }
  3187. if( hasRight )
  3188. {
  3189. //----------- Right, exit_direction=5 -----------//
  3190. if(node_type&0x2 && can_move_to(upperLeftX+2,upperLeftY) &&
  3191. node_type&0x8 && can_move_to(upperLeftX+2,upperLeftY+1))
  3192. {
  3193. if(parentEnterDirection==3 || parentEnterDirection==4 || parentEnterDirection==6 || parentEnterDirection==7)
  3194. cost = 1;
  3195. else
  3196. {
  3197. if(parentEnterDirection==1 ||
  3198. (parentEnterDirection==2 && (node_type&0x1 || can_move_to(upperLeftX+1,upperLeftY+2))) ||
  3199. (parentEnterDirection==8 && (node_type&0x4 || can_move_to(upperLeftX+1,upperLeftY-1))))
  3200. cost = 2;
  3201. else
  3202. {
  3203. if(parentEnterDirection==0 && !source_blocked_exit_direction[4] && //direction 5
  3204. (source_locate_type==1||source_locate_type==3))
  3205. cost = 1;
  3206. else
  3207. cost = 0;
  3208. }
  3209. }
  3210. if(cost||(parentEnterDirection==0&&!source_blocked_exit_direction[4]&&(source_locate_type==2||source_locate_type==4)))
  3211. {
  3212. err_when(parentEnterDirection==5);
  3213. if(generate_succ(node_x+1,node_y,1,cost))
  3214. return 1;
  3215. }
  3216. }
  3217. if( hasUp )
  3218. {
  3219. //-------- Upper-Right, exit_direction=6 ---------//
  3220. if(can_move_to(upperLeftX+1,upperLeftY-1) && can_move_to(upperLeftX+2,upperLeftY-1) &&
  3221. node_type&0x2 && can_move_to(upperLeftX+2,upperLeftY))
  3222. {
  3223. if(parentEnterDirection==4 || parentEnterDirection==5 || parentEnterDirection==7 || parentEnterDirection==8)
  3224. cost = 1;
  3225. else
  3226. {
  3227. if((parentEnterDirection==1 && (node_type&0x8 || can_move_to(upperLeftX,upperLeftY-1))) ||
  3228. (parentEnterDirection==2 && node_type==15) ||
  3229. (parentEnterDirection==3 && (node_type&0x1 || can_move_to(upperLeftX+2,upperLeftY+1))))
  3230. cost = 2;
  3231. else
  3232. {
  3233. if(parentEnterDirection==0 && !source_blocked_exit_direction[5]) // direction 6
  3234. {
  3235. if(source_locate_type==1||source_locate_type==2)
  3236. cost = 1;
  3237. else
  3238. cost = 2;
  3239. }
  3240. else
  3241. cost = 0;
  3242. }
  3243. }
  3244. if(cost)
  3245. {
  3246. err_when(parentEnterDirection==6);
  3247. if(generate_succ(node_x+1,node_y-1,2,cost))
  3248. return 1;
  3249. }
  3250. }
  3251. }
  3252. if( hasDown )
  3253. {
  3254. //--------- Lower-Right, exit_direction=4 ---------//
  3255. if(node_type&0x8 && can_move_to(upperLeftX+2,upperLeftY+1) &&
  3256. can_move_to(upperLeftX+1,upperLeftY+2) && can_move_to(upperLeftX+2,upperLeftY+2))
  3257. {
  3258. if(parentEnterDirection==2 || parentEnterDirection==3 || parentEnterDirection==5 || parentEnterDirection==6)
  3259. cost = 1;
  3260. else
  3261. {
  3262. if((parentEnterDirection==1 && (node_type&0x2 || can_move_to(upperLeftX,upperLeftY+2))) ||
  3263. (parentEnterDirection==8 && node_type==15) ||
  3264. (parentEnterDirection==7 && (node_type&0x4 || can_move_to(upperLeftX+2,upperLeftY))))
  3265. cost = 2;
  3266. else
  3267. {
  3268. if(parentEnterDirection==0&&source_locate_type!=4&&!source_blocked_exit_direction[3]) // direction 4
  3269. cost = 1;
  3270. else
  3271. cost = 0;
  3272. }
  3273. }
  3274. if(cost||(parentEnterDirection==0&&source_locate_type==4&&!source_blocked_exit_direction[3]))
  3275. {
  3276. err_when(parentEnterDirection==4);
  3277. if(generate_succ(node_x+1,node_y+1,8,cost))
  3278. return 1;
  3279. }
  3280. }
  3281. }
  3282. }
  3283. if( hasUp )
  3284. {
  3285. //---------- Upper, exit_direction=7 -----------//
  3286. if(can_move_to(upperLeftX,upperLeftY-1) && can_move_to(upperLeftX+1,upperLeftY-1) &&
  3287. node_type&0x1 && node_type&0x2)
  3288. {
  3289. if(parentEnterDirection==1 || parentEnterDirection==5 || parentEnterDirection==6 || parentEnterDirection==8)
  3290. cost = 1;
  3291. else
  3292. {
  3293. if((parentEnterDirection==2 && (node_type&0x8 || can_move_to(upperLeftX-1,upperLeftY))) ||
  3294. parentEnterDirection==3 ||
  3295. (parentEnterDirection==4 && (node_type&0x4 || can_move_to(upperLeftX+2,upperLeftY))))
  3296. cost = 2;
  3297. else
  3298. {
  3299. if(parentEnterDirection==0 && !source_blocked_exit_direction[6]) // direction 7
  3300. {
  3301. if(source_locate_type==1||source_locate_type==2)
  3302. cost = 1;
  3303. else
  3304. cost = 2;
  3305. }
  3306. else
  3307. cost = 0;
  3308. }
  3309. }
  3310. if(cost)
  3311. {
  3312. err_when(parentEnterDirection==7);
  3313. if(generate_succ(node_x,node_y-1,3,cost))
  3314. return 1;
  3315. }
  3316. }
  3317. }
  3318. if( hasDown )
  3319. {
  3320. //---------- Lower, exit_direction=3 -----------//
  3321. if(node_type&0x4 && node_type&0x8 &&
  3322. can_move_to(upperLeftX,upperLeftY+2) && can_move_to(upperLeftX+1,upperLeftY+2))
  3323. {
  3324. if(parentEnterDirection==1 || parentEnterDirection==2 || parentEnterDirection==4 || parentEnterDirection==5)
  3325. cost = 1;
  3326. else
  3327. {
  3328. if((parentEnterDirection==6 && (node_type&0x1 || can_move_to(upperLeftX+2,upperLeftY+1))) ||
  3329. parentEnterDirection==7 ||
  3330. (parentEnterDirection==8 && (node_type&0x2 || can_move_to(upperLeftX-1,upperLeftY+1))))
  3331. cost = 2;
  3332. else
  3333. {
  3334. if(parentEnterDirection==0&&(source_locate_type==1||source_locate_type==2)&&!source_blocked_exit_direction[2]) // direction 3
  3335. cost = 1;
  3336. else
  3337. cost = 0;
  3338. }
  3339. }
  3340. if(cost||(parentEnterDirection==0&&(source_locate_type==3||source_locate_type==4)&&!source_blocked_exit_direction[2]))
  3341. {
  3342. err_when(parentEnterDirection==3);
  3343. if(generate_succ(node_x,node_y+1,7,cost))
  3344. return 1;
  3345. }
  3346. }
  3347. }
  3348. return 0;
  3349. }
  3350. //------- End of function NodeS2::generate_successors -------//
  3351. //-------- Begin of function NodeS2::generate_succ ---------//
  3352. short NodeS2::generate_succ(short x, short y, short enter_direct,short cost)
  3353. {
  3354. int upperLeftX, upperLeftY;
  3355. upperLeftX = x<<1;
  3356. upperLeftY = y<<1;
  3357. //----- if it points back to this node's parent ----//
  3358. if( parent_node )
  3359. {
  3360. if( parent_node->node_x==x && parent_node->node_y==y )
  3361. return 0;
  3362. }
  3363. //----- if there is an existing node at the given position ----//
  3364. short c, g = node_g+cost; // g(Successor)=g(BestNode)+cost of getting from BestNode to Successor
  3365. short nodeRecno;
  3366. //if( (nodeRecno=cur_node_matrix_s2[y*(MAX_WORLD_X_LOC/2+1)+x]) > 0 )
  3367. if( (nodeRecno=cur_node_matrix_s2[y*(MAX_WORLD_X_LOC/2+1)+x]) > 0 &&
  3368. nodeRecno<max_node_num_s2)
  3369. {
  3370. NodeS2* oldNode = cur_node_array_s2+nodeRecno-1;
  3371. //------ Add oldNode to the list of BestNode's child_noderen (or Successors).
  3372. for(c=0 ; c<MAX_CHILD_NODE && child_node[c] ; c++);
  3373. child_node[c]=oldNode;
  3374. //---- if our new g value is < oldNode's then reset oldNode's parent to point to BestNode
  3375. if (g < oldNode->node_g)
  3376. {
  3377. //-------------check whether the oldNode can process propagate down -----------//
  3378. char canPropagateDown=1;
  3379. if(oldNode->node_type!=15 && enter_direct%2==0)
  3380. {
  3381. NodeS2* oldChildNode;
  3382. short xShift1 = x_shift_array[enter_direct-1];
  3383. short yShift1 = y_shift_array[enter_direct-1];
  3384. short xShift2, yShift2, xShift3, yShift3;
  3385. if(enter_direct>1)
  3386. {
  3387. xShift2 = x_shift_array[enter_direct-2];
  3388. yShift2 = y_shift_array[enter_direct-2];
  3389. }
  3390. else
  3391. {
  3392. xShift2 = x_shift_array[7];
  3393. yShift2 = y_shift_array[7];
  3394. }
  3395. if(enter_direct<8)
  3396. {
  3397. xShift3 = x_shift_array[enter_direct];
  3398. yShift3 = y_shift_array[enter_direct];
  3399. }
  3400. else
  3401. {
  3402. xShift3 = x_shift_array[0];
  3403. yShift3 = y_shift_array[0];
  3404. }
  3405. for(c=0; c<MAX_CHILD_NODE && oldNode->child_node[c]; c++) // for each child of oldNode
  3406. {
  3407. oldChildNode = oldNode->child_node[c];
  3408. if(oldNode->parent_node != oldChildNode)
  3409. {
  3410. switch(enter_direct) // for entering at corner
  3411. {
  3412. case 2:
  3413. if(oldChildNode->enter_direction==enter_direct &&
  3414. oldChildNode->node_x-oldNode->node_x==xShift1 &&
  3415. oldChildNode->node_y-oldNode->node_y==yShift1 &&
  3416. (!can_move_to(upperLeftX, upperLeftY) ||
  3417. !can_move_to(upperLeftX+1, upperLeftY+1)))
  3418. canPropagateDown = 0;
  3419. if(oldChildNode->enter_direction==enter_direct-1 &&
  3420. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3421. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3422. !can_move_to(upperLeftX,upperLeftY) &&
  3423. !can_move_to(upperLeftX+1,upperLeftY+2))
  3424. canPropagateDown = 0;
  3425. if(oldChildNode->enter_direction==enter_direct+1 &&
  3426. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3427. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3428. !can_move_to(upperLeftX+1,upperLeftY+1) &&
  3429. !can_move_to(upperLeftX-1,upperLeftY))
  3430. canPropagateDown = 0;
  3431. break;
  3432. case 4:
  3433. if(oldChildNode->enter_direction==enter_direct &&
  3434. oldChildNode->node_x-oldNode->node_x==xShift1 &&
  3435. oldChildNode->node_y-oldNode->node_y==yShift1 &&
  3436. (!can_move_to(upperLeftX+1, upperLeftY) ||
  3437. !can_move_to(upperLeftX, upperLeftY+1)))
  3438. canPropagateDown = 0;
  3439. if(oldChildNode->enter_direction==enter_direct-1 &&
  3440. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3441. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3442. !can_move_to(upperLeftX,upperLeftY+1)&&
  3443. !can_move_to(upperLeftX+2,upperLeftY))
  3444. canPropagateDown = 0;
  3445. if(oldChildNode->enter_direction==enter_direct+1 &&
  3446. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3447. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3448. !can_move_to(upperLeftX,upperLeftY+2) &&
  3449. !can_move_to(upperLeftX+1,upperLeftY))
  3450. canPropagateDown = 0;
  3451. break;
  3452. case 6:
  3453. if(oldChildNode->enter_direction==enter_direct &&
  3454. oldChildNode->node_x-oldNode->node_x==xShift1 &&
  3455. oldChildNode->node_y-oldNode->node_y==yShift1 &&
  3456. (!can_move_to(upperLeftX, upperLeftY) ||
  3457. !can_move_to(upperLeftX+1, upperLeftY+1)))
  3458. canPropagateDown = 0;
  3459. if(oldChildNode->enter_direction==enter_direct-1 &&
  3460. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3461. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3462. !can_move_to(upperLeftX,upperLeftY-1)&&
  3463. !can_move_to(upperLeftX+1,upperLeftY+1))
  3464. canPropagateDown = 0;
  3465. if(oldChildNode->enter_direction==enter_direct+1 &&
  3466. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3467. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3468. !can_move_to(upperLeftX,upperLeftY) &&
  3469. !can_move_to(upperLeftX+2,upperLeftY+1))
  3470. canPropagateDown = 0;
  3471. break;
  3472. case 8:
  3473. if(oldChildNode->enter_direction==enter_direct &&
  3474. oldChildNode->node_x-oldNode->node_x==xShift1 &&
  3475. oldChildNode->node_y-oldNode->node_y==yShift1 &&
  3476. (!can_move_to(upperLeftX+1, upperLeftY) ||
  3477. !can_move_to(upperLeftX, upperLeftY+1)))
  3478. canPropagateDown = 0;
  3479. if(oldChildNode->enter_direction==enter_direct-1 &&
  3480. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3481. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3482. !can_move_to(upperLeftX-1,upperLeftY+1)&&
  3483. !can_move_to(upperLeftX+1,upperLeftY))
  3484. canPropagateDown = 0;
  3485. if(oldChildNode->enter_direction==(enter_direct%8+1) &&
  3486. oldChildNode->node_x-oldNode->node_x==xShift2 &&
  3487. oldChildNode->node_x-oldNode->node_y==yShift2 &&
  3488. !can_move_to(upperLeftX+1,upperLeftY-1) &&
  3489. !can_move_to(upperLeftX,upperLeftY+1))
  3490. canPropagateDown = 0;
  3491. break;
  3492. default: err_here();
  3493. break;
  3494. }
  3495. }
  3496. }
  3497. }
  3498. if(canPropagateDown)
  3499. {
  3500. oldNode->parent_node = this;
  3501. oldNode->node_g = g;
  3502. oldNode->node_f = g+oldNode->node_h;
  3503. oldNode->enter_direction = (char)enter_direct;
  3504. //-------- if it's a closed node ---------//
  3505. if( oldNode->child_node[0] )
  3506. {
  3507. //-------------------------------------------------//
  3508. // Since we changed the g value of oldNode, we need
  3509. // to propagate this new value downwards, i.e.
  3510. // do a Depth-First traversal of the tree!
  3511. //-------------------------------------------------//
  3512. oldNode->propagate_down();
  3513. sys_yield();
  3514. }
  3515. }
  3516. }
  3517. }
  3518. else //------------ add a new node -----------//
  3519. {
  3520. NodeS2* succNode = cur_node_array_s2 + cur_seek_path_s2->node_count++;
  3521. memset(succNode, 0, sizeof(NodeS2));
  3522. succNode->parent_node = this;
  3523. succNode->node_g = g;
  3524. succNode->node_h = (x-cur_dest_x_s2)*(x-cur_dest_x_s2)+(y-cur_dest_y_s2)*(y-cur_dest_y_s2); // should do sqrt(), but since we don't really
  3525. succNode->node_f = g+succNode->node_h; // care about the distance but just which branch looks
  3526. succNode->node_x = x; // better this should suffice. Anyayz it's faster.
  3527. succNode->node_y = y;
  3528. succNode->enter_direction = (char)enter_direct;
  3529. succNode->node_type =can_move_to(upperLeftX,upperLeftY)+(can_move_to(upperLeftX+1,upperLeftY)<<1)+
  3530. (can_move_to(upperLeftX,upperLeftY+1)<<2)+(can_move_to(upperLeftX+1,upperLeftY+1)<<3);
  3531. /*if(search_mode_s2==4 && nodeRecno>max_node_num_s2)
  3532. {
  3533. reuse_connect_node_exit_direction = nodeRecno - max_node_num_s2;
  3534. reuse_result_node_ptr_s2 = succNode;
  3535. return 1;
  3536. }*/
  3537. if(search_mode_s2==SEARCH_MODE_REUSE && nodeRecno>max_node_num_s2) // path-reuse node found, but checking of can_walk(final_dest_?) is requested
  3538. {
  3539. int destIndex = nodeRecno - max_node_num_s2;
  3540. int addNode = 1;
  3541. switch(destIndex)
  3542. {
  3543. case 1:
  3544. final_dest_x_s2 = x<<1;
  3545. final_dest_y_s2 = y<<1;
  3546. break;
  3547. case 2:
  3548. if(enter_direct==2 && (!succNode->node_type&0x1 || !can_move_to((x<<1)+1,(y<<1)+2) ) )
  3549. addNode = 0;
  3550. if(addNode && enter_direct==8 && (!succNode->node_type&0x4 || !can_move_to((x<<1)+1,(y<<1)-1) ) )
  3551. addNode = 0;
  3552. if(addNode) // not the two blocked cases
  3553. {
  3554. final_dest_x_s2 = (x<<1) + 1;
  3555. final_dest_y_s2 = y<<1;
  3556. }
  3557. break;
  3558. case 3:
  3559. if(enter_direct==6 && (!succNode->node_type&0x1 || !can_move_to((x<<1)+2,(y<<1)+1) ) )
  3560. addNode = 0;
  3561. if(addNode && enter_direct==8 && (!succNode->node_type&0x2 || !can_move_to((x<<1)-1,(y<<1)+1) ) )
  3562. addNode = 0;
  3563. if(addNode) // not the two blocked cases
  3564. {
  3565. final_dest_x_s2 = x<<1;
  3566. final_dest_y_s2 = (y<<1)+1;
  3567. }
  3568. break;
  3569. case 4:
  3570. if(enter_direct==1 && (!succNode->node_type&0x2 || !can_move_to(x<<1,(y<<1)+2) ) )
  3571. addNode = 0;
  3572. if(addNode && enter_direct==7 && (!succNode->node_type&0x4 || !can_move_to((x<<1)+2,y<<1) ) )
  3573. addNode = 0;
  3574. if(addNode && enter_direct==8 && (!succNode->node_type&0x2 || !succNode->node_type&0x4) )
  3575. addNode = 0;
  3576. if(addNode) // not the two blocked cases
  3577. {
  3578. final_dest_x_s2 = (x<<1)+1;
  3579. final_dest_y_s2 = (y<<1)+1;
  3580. }
  3581. break;
  3582. default: err_here();
  3583. break;
  3584. }
  3585. if(addNode)
  3586. {
  3587. reuse_result_node_ptr_s2 = succNode;
  3588. err_when(!can_move_to_s2(final_dest_x_s2, final_dest_y_s2));
  3589. return 1; // PATH_REUSE_FOUND
  3590. }
  3591. else // add nothing
  3592. {
  3593. memset(succNode, 0, sizeof(NodeS2));
  3594. cur_seek_path_s2->node_count--;
  3595. return 0;
  3596. }
  3597. }
  3598. cur_node_matrix_s2[y*(MAX_WORLD_X_LOC/2+1)+x] = cur_seek_path_s2->node_count;
  3599. cur_seek_path_s2->insert_open_node(succNode); // insert succNode on open_node_list list wrt f
  3600. for(c=0 ; c<MAX_CHILD_NODE && child_node[c] ; c++); // Add oldNode to the list of BestNode's child_noderen (or succNodes).
  3601. child_node[c]=succNode;
  3602. }
  3603. return 0;
  3604. }
  3605. //------- End of function NodeS2::generate_succ -------//
  3606. //-------- Begin of function NodeS2::propagate_down ---------//
  3607. void NodeS2::propagate_down()
  3608. {
  3609. NodeS2 *childNode, *fatherNode;
  3610. int c, g=node_g; // alias.
  3611. short cost;
  3612. char xShift, yShift; // the x, y difference between parent and child nodes
  3613. char childEnterDirection;
  3614. char exitDirection;
  3615. char testResult;
  3616. for(c=0;c<8;c++)
  3617. {
  3618. if ((childNode=child_node[c])==NULL) // create alias for faster access.
  3619. break;
  3620. if(childNode->parent_node==this)
  3621. break;
  3622. cost = 2; // in fact, may be 1 or 2
  3623. if (g+cost <= childNode->node_g) // first checking
  3624. {
  3625. char canMove = 0;
  3626. int childUpperLeftX = childNode->node_x<<1;
  3627. int childUpperLeftY = childNode->node_y<<1;
  3628. xShift = childNode->node_x-node_x;
  3629. yShift = childNode->node_y-node_y;
  3630. err_when(abs(xShift)>1 || abs(yShift)>1);
  3631. //---------- calulate the new enter direction ------------//
  3632. switch(yShift)
  3633. {
  3634. case -1:
  3635. childEnterDirection = char(3-xShift);
  3636. break;
  3637. case 0:
  3638. childEnterDirection = char(3-(xShift<<1));
  3639. break;
  3640. case 1:
  3641. childEnterDirection = char(7+xShift);
  3642. break;
  3643. }
  3644. //---------- decide can move or not and use special case or not --------//
  3645. switch(childEnterDirection)
  3646. {
  3647. case 1:
  3648. err_when(node_x!=childNode->node_x-1 || node_y!=childNode->node_y);
  3649. if(can_move_to(childUpperLeftX-1,childUpperLeftY) && childNode->node_type&0x1 &&
  3650. can_move_to(childUpperLeftX-1,childUpperLeftY+1) && childNode->node_type&0x4) //if the entering location is walkable
  3651. {
  3652. switch(enter_direction)
  3653. {
  3654. case 5: canMove = 0; //reverse direction
  3655. break;
  3656. case 1:case 3:case 4:case 6:case 7: // obviously
  3657. canMove = 1;
  3658. break;
  3659. case 2: if(node_type&0x1 || can_move_to(childUpperLeftX-1, childUpperLeftY+2))
  3660. canMove = 1;
  3661. break;
  3662. case 8: if(node_type&0x4 || can_move_to(childUpperLeftX-1, childUpperLeftY-1))
  3663. canMove = 1;
  3664. break;
  3665. default: err_here();
  3666. break;
  3667. }
  3668. }
  3669. break;
  3670. case 2:
  3671. err_when(node_x!=childNode->node_x-1 || node_y!=childNode->node_y+1);
  3672. if(can_move_to(childUpperLeftX-1,childUpperLeftY+1) && childNode->node_type&0x4 &&
  3673. can_move_to(childUpperLeftX-1,childUpperLeftY+2) && can_move_to(childUpperLeftX,childUpperLeftY+2))
  3674. {
  3675. switch(enter_direction)
  3676. {
  3677. case 6: canMove = 0; //reverse direction
  3678. break;
  3679. case 4:case 5:case 7:case 8:
  3680. canMove = 1;
  3681. break;
  3682. case 1: if(node_type&0x8 || can_move_to(childUpperLeftX-2, childUpperLeftY+1))
  3683. canMove = 1;
  3684. break;
  3685. case 2: if(node_type==15)
  3686. canMove = 1;
  3687. else
  3688. canMove = 0;
  3689. break;
  3690. case 3: if(node_type&0x1 || can_move_to(childUpperLeftX, childUpperLeftY+3))
  3691. canMove = 1;
  3692. break;
  3693. default: err_here();
  3694. break;
  3695. }
  3696. }
  3697. break;
  3698. case 3:
  3699. err_when(node_x!=childNode->node_x || node_y!=childNode->node_y+1);
  3700. if(childNode->node_type&0x4 && childNode->node_type&0x8 &&
  3701. can_move_to(childUpperLeftX,childUpperLeftY+2) && can_move_to(childUpperLeftX+1,childUpperLeftY+2))
  3702. {
  3703. switch(enter_direction)
  3704. {
  3705. case 7: canMove = 0; //reverse direction
  3706. break;
  3707. case 1:case 3:case 5:case 6:case 8:
  3708. canMove = 1;
  3709. break;
  3710. case 2: if(node_type&0x8 || can_move_to(childUpperLeftX-1, childUpperLeftY+2))
  3711. canMove = 1;
  3712. break;
  3713. case 4: if(node_type&0x4 || can_move_to(childUpperLeftX+2, childUpperLeftY+2))
  3714. canMove = 1;
  3715. break;
  3716. default: err_here();
  3717. break;
  3718. }
  3719. }
  3720. break;
  3721. case 4:
  3722. err_when(node_x!=childNode->node_x+1 || node_y!=childNode->node_y+1);
  3723. if(childNode->node_type&0x8 && can_move_to(childUpperLeftX+2,childUpperLeftY+1) &&
  3724. can_move_to(childUpperLeftX+1,childUpperLeftY+2) && can_move_to(childUpperLeftX+2,childUpperLeftY+2))
  3725. {
  3726. switch(enter_direction)
  3727. {
  3728. case 8: canMove = 0; //reverse direction
  3729. break;
  3730. case 1:case 2:case 6:case 7:
  3731. canMove = 1;
  3732. break;
  3733. case 3: if(node_type&0x2 || can_move_to(childUpperLeftX+1, childUpperLeftY+3))
  3734. canMove = 1;
  3735. break;
  3736. case 4: if(node_type==15)
  3737. canMove = 1;
  3738. else
  3739. canMove = 0;
  3740. break;
  3741. case 5: if(node_type&0x4 || can_move_to(childUpperLeftX+3, childUpperLeftY+1))
  3742. canMove = 1;
  3743. break;
  3744. default: err_here();
  3745. break;
  3746. }
  3747. }
  3748. break;
  3749. case 5:
  3750. err_when(node_x!=childNode->node_x+1 || node_y!=childNode->node_y);
  3751. if(childNode->node_type&0x2 && can_move_to(childUpperLeftX+2,childUpperLeftY) &&
  3752. childNode->node_type&0x8 && can_move_to(childUpperLeftX+2,childUpperLeftY+1))
  3753. {
  3754. switch(enter_direction)
  3755. {
  3756. case 1: canMove = 0; //reverse direction
  3757. break;
  3758. case 2:case 3:case 5:case 7:case 8:
  3759. canMove = 1;
  3760. break;
  3761. case 4: if(node_type&0x2 || can_move_to(childUpperLeftX+2, childUpperLeftY+2))
  3762. canMove = 1;
  3763. break;
  3764. case 6: if(node_type&0x8 || can_move_to(childUpperLeftX+2, childUpperLeftY-1))
  3765. canMove = 1;
  3766. break;
  3767. default: err_here();
  3768. break;
  3769. }
  3770. }
  3771. break;
  3772. case 6:
  3773. err_when(node_x!=childNode->node_x+1 || node_y!=childNode->node_y-1);
  3774. if(can_move_to(childUpperLeftX+1,childUpperLeftY-1) && can_move_to(childUpperLeftX+2,childUpperLeftY-1) &&
  3775. childNode->node_type&0x2 && can_move_to(childUpperLeftX+2,childUpperLeftY))
  3776. {
  3777. switch(enter_direction)
  3778. {
  3779. case 2: canMove = 0; //reverse direction
  3780. break;
  3781. case 1:case 3:case 4:case 8:
  3782. canMove = 1;
  3783. break;
  3784. case 5: if(node_type&0x1 || can_move_to(childUpperLeftX+3, childUpperLeftY))
  3785. canMove = 1;
  3786. break;
  3787. case 6: if(node_type==15)
  3788. canMove = 1;
  3789. else
  3790. canMove = 0;
  3791. break;
  3792. case 7: if(node_type&0x8 || can_move_to(childUpperLeftX+1, childUpperLeftY-2))
  3793. canMove = 1;
  3794. break;
  3795. default: err_here();
  3796. break;
  3797. }
  3798. }
  3799. break;
  3800. case 7:
  3801. err_when(node_x!=childNode->node_x || node_y!=childNode->node_y-1);
  3802. if(can_move_to(childUpperLeftX,childUpperLeftY-1) && can_move_to(childUpperLeftX+1,childUpperLeftY-1) &&
  3803. childNode->node_type&0x1 && childNode->node_type&0x2)
  3804. {
  3805. switch(enter_direction)
  3806. {
  3807. case 3: canMove = 0; //reverse direction
  3808. break;
  3809. case 1:case 2:case 4:case 5:case 7:
  3810. canMove = 1;
  3811. break;
  3812. case 6: if(node_type&0x1 || can_move_to(childUpperLeftX+2, childUpperLeftY-1))
  3813. canMove = 1;
  3814. break;
  3815. case 8: if(node_type&0x2 || can_move_to(childUpperLeftX-1, childUpperLeftY-1))
  3816. canMove = 1;
  3817. break;
  3818. default: err_here();
  3819. break;
  3820. }
  3821. }
  3822. break;
  3823. case 8:
  3824. err_when(node_x!=childNode->node_x-1 || node_y!=childNode->node_y-1);
  3825. if(can_move_to(childUpperLeftX-1,childUpperLeftY-1) && can_move_to(childUpperLeftX,childUpperLeftY-1) &&
  3826. can_move_to(childUpperLeftX-1,childUpperLeftY) && childNode->node_type&0x1)//can_move_to(childUpperLeftX,childUpperLeftY))
  3827. {
  3828. switch(enter_direction)
  3829. {
  3830. case 4: canMove = 0; //reverse direction
  3831. break;
  3832. case 2:case 3:case 5:case 6:
  3833. canMove = 1;
  3834. break;
  3835. case 1: if(node_type&0x2 || can_move_to(childUpperLeftX-2, childUpperLeftY))
  3836. canMove = 1;
  3837. break;
  3838. case 7: if(node_type&0x4 || can_move_to(childUpperLeftX, childUpperLeftY-2))
  3839. canMove = 1;
  3840. break;
  3841. case 8: if(node_type==15)
  3842. canMove = 1;
  3843. else
  3844. canMove = 0;
  3845. break;
  3846. default: err_here();
  3847. break;
  3848. }
  3849. }
  3850. break;
  3851. default: err_here();
  3852. break;
  3853. }
  3854. if(canMove) // can move in the child node
  3855. {
  3856. exitDirection = (childEnterDirection+3)%8+1;
  3857. if(enter_direction > exitDirection)
  3858. {
  3859. if((enter_direction==8 && (exitDirection==1 || exitDirection==2)) ||
  3860. (enter_direction==7 && exitDirection==1))
  3861. testResult = exitDirection + 8 - enter_direction;
  3862. else
  3863. testResult = enter_direction - exitDirection;
  3864. }
  3865. else
  3866. {
  3867. if((exitDirection==8 && (enter_direction==1 || enter_direction==2)) ||
  3868. (exitDirection==7 && enter_direction==1))
  3869. testResult = enter_direction + 8 - exitDirection;
  3870. else
  3871. testResult = exitDirection - enter_direction;
  3872. }
  3873. if((exitDirection%2 && testResult<=2) || (!exitDirection%2 && testResult<=1))
  3874. cost = 1;
  3875. else
  3876. cost = 2;
  3877. err_when(cost>2 || cost<1);
  3878. if(g+cost < childNode->node_g) // second checking, mainly for cost = 2;
  3879. {
  3880. char canPropagateDown=1;
  3881. if(childNode->node_type!=15 && childEnterDirection%2==0)
  3882. {
  3883. NodeS2* oldChildNode;
  3884. short xShift1 = x_shift_array[childEnterDirection-1];
  3885. short yShift1 = y_shift_array[childEnterDirection-1];
  3886. short xShift2, yShift2, xShift3, yShift3;
  3887. if(childEnterDirection>1)
  3888. {
  3889. xShift2 = x_shift_array[childEnterDirection-2];
  3890. yShift2 = y_shift_array[childEnterDirection-2];
  3891. }
  3892. else
  3893. {
  3894. xShift2 = x_shift_array[7];
  3895. yShift2 = y_shift_array[7];
  3896. }
  3897. if(childEnterDirection<8)
  3898. {
  3899. xShift3 = x_shift_array[childEnterDirection];
  3900. yShift3 = y_shift_array[childEnterDirection];
  3901. }
  3902. else
  3903. {
  3904. xShift3 = x_shift_array[0];
  3905. yShift3 = y_shift_array[0];
  3906. }
  3907. for(c=0; c<MAX_CHILD_NODE && childNode->child_node[c]; c++) // for each child of oldNode
  3908. {
  3909. oldChildNode = childNode->child_node[c];
  3910. if(childNode->parent_node != oldChildNode)
  3911. {
  3912. switch(childEnterDirection)
  3913. {
  3914. case 2:
  3915. if(oldChildNode->enter_direction==childEnterDirection &&
  3916. oldChildNode->node_x-childNode->node_x==xShift1 &&
  3917. oldChildNode->node_y-childNode->node_y==yShift1)
  3918. canPropagateDown = 0;
  3919. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  3920. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3921. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3922. !can_move_to(childUpperLeftX,childUpperLeftY)&&
  3923. !can_move_to(childUpperLeftX+1,childUpperLeftY+2))
  3924. canPropagateDown = 0;
  3925. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  3926. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3927. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3928. !can_move_to(childUpperLeftX+1,childUpperLeftY+1) &&
  3929. !can_move_to(childUpperLeftX-1,childUpperLeftY))
  3930. canPropagateDown = 0;
  3931. break;
  3932. case 4:
  3933. if(oldChildNode->enter_direction==childEnterDirection &&
  3934. oldChildNode->node_x-childNode->node_x==xShift1 &&
  3935. oldChildNode->node_y-childNode->node_y==yShift1)
  3936. canPropagateDown = 0;
  3937. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  3938. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3939. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3940. !can_move_to(childUpperLeftX,childUpperLeftY+1)&&
  3941. !can_move_to(childUpperLeftX+2,childUpperLeftY))
  3942. canPropagateDown = 0;
  3943. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  3944. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3945. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3946. !can_move_to(childUpperLeftX,childUpperLeftY+2) &&
  3947. !can_move_to(childUpperLeftX+1,childUpperLeftY))
  3948. canPropagateDown = 0;
  3949. break;
  3950. case 6:
  3951. if(oldChildNode->enter_direction==childEnterDirection &&
  3952. oldChildNode->node_x-childNode->node_x==xShift1 &&
  3953. oldChildNode->node_y-childNode->node_y==yShift1)
  3954. canPropagateDown = 0;
  3955. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  3956. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3957. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3958. !can_move_to(childUpperLeftX,childUpperLeftY-1)&&
  3959. !can_move_to(childUpperLeftX+1,childUpperLeftY+1))
  3960. canPropagateDown = 0;
  3961. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  3962. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3963. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3964. !can_move_to(childUpperLeftX,childUpperLeftY) &&
  3965. !can_move_to(childUpperLeftX+2,childUpperLeftY+1))
  3966. canPropagateDown = 0;
  3967. break;
  3968. case 8:
  3969. if(oldChildNode->enter_direction==childEnterDirection &&
  3970. oldChildNode->node_x-childNode->node_x==xShift1 &&
  3971. oldChildNode->node_y-childNode->node_y==yShift1)
  3972. canPropagateDown = 0;
  3973. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  3974. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3975. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3976. !can_move_to(childUpperLeftX-1,childUpperLeftY+1)&&
  3977. !can_move_to(childUpperLeftX+1,childUpperLeftY))
  3978. canPropagateDown = 0;
  3979. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  3980. oldChildNode->node_x-childNode->node_x==xShift2 &&
  3981. oldChildNode->node_x-childNode->node_y==yShift2 &&
  3982. !can_move_to(childUpperLeftX+1,childUpperLeftY-1) &&
  3983. !can_move_to(childUpperLeftX,childUpperLeftY+1))
  3984. canPropagateDown = 0;
  3985. break;
  3986. default: err_here();
  3987. break;
  3988. }
  3989. }
  3990. }
  3991. }
  3992. if(canPropagateDown)
  3993. {
  3994. childNode->node_g = g+cost;
  3995. childNode->node_f = childNode->node_g+childNode->node_h;
  3996. childNode->parent_node = this;// reset parent to new path.
  3997. childNode->enter_direction = childEnterDirection;
  3998. stack_push(childNode); // Now the childNode's branch need to be checked out. Remember the new cost must be propagated down.
  3999. }
  4000. }
  4001. }
  4002. }
  4003. }
  4004. while( cur_stack_pos_s2>0 )
  4005. {
  4006. fatherNode=stack_pop();
  4007. g = fatherNode->node_g;
  4008. for(c=0;c<8;c++)
  4009. {
  4010. if ((childNode=fatherNode->child_node[c])==NULL) // we may stop the propagation 2 ways: either
  4011. break;
  4012. if(childNode->parent_node==fatherNode)
  4013. break;
  4014. cost = 2; // in fact, may be 1 or 2
  4015. if (g+cost <= childNode->node_g) // first checking
  4016. // there are no children, or that the g value of the child is equal or better than the cost we're propagating
  4017. {
  4018. char canMove = 0;
  4019. int childUpperLeftX = childNode->node_x<<1;
  4020. int childUpperLeftY = childNode->node_y<<1;
  4021. xShift = childNode->node_x-fatherNode->node_x;
  4022. yShift = childNode->node_y-fatherNode->node_y;
  4023. err_when(abs(xShift)>1 || abs(yShift)>1);
  4024. //---------- calulate the new enter direction ------------//
  4025. switch(yShift)
  4026. {
  4027. case -1:
  4028. childEnterDirection = char(3-xShift);
  4029. break;
  4030. case 0:
  4031. childEnterDirection = char(3-(xShift<<1));
  4032. break;
  4033. case 1:
  4034. childEnterDirection = char(7+xShift);
  4035. break;
  4036. }
  4037. switch(childEnterDirection)
  4038. {
  4039. case 1:
  4040. err_when(fatherNode->node_x!=childNode->node_x-1 || fatherNode->node_y!=childNode->node_y);
  4041. if(can_move_to(childUpperLeftX-1,childUpperLeftY) && childNode->node_type&0x1 && //can_move_to(childUpperLeftX,childUpperLeftY) &&
  4042. can_move_to(childUpperLeftX-1,childUpperLeftY+1) && childNode->node_type&0x4)//can_move_to(childUpperLeftX,childUpperLeftY+1))
  4043. {
  4044. switch(fatherNode->enter_direction)
  4045. {
  4046. case 5: canMove = 0; //reverse direction
  4047. break;
  4048. case 1:case 3:case 4:case 6:case 7:
  4049. canMove = 1;
  4050. break;
  4051. case 2: if(fatherNode->node_type&0x1 || can_move_to(childUpperLeftX-1, childUpperLeftY+2))
  4052. canMove = 1;
  4053. break;
  4054. case 8: if(fatherNode->node_type&0x4 || can_move_to(childUpperLeftX-1, childUpperLeftY-1))
  4055. canMove = 1;
  4056. break;
  4057. default: err_here();
  4058. break;
  4059. }
  4060. }
  4061. break;
  4062. case 2:
  4063. err_when(fatherNode->node_x!=childNode->node_x-1 || fatherNode->node_y!=childNode->node_y+1);
  4064. if(can_move_to(childUpperLeftX-1,childUpperLeftY+1) && childNode->node_type&0x4 &&//can_move_to(childUpperLeftX,childUpperLeftY+1) &&
  4065. can_move_to(childUpperLeftX-1,childUpperLeftY+2) && can_move_to(childUpperLeftX,childUpperLeftY+2))
  4066. {
  4067. switch(fatherNode->enter_direction)
  4068. {
  4069. case 6: canMove = 0; //reverse direction
  4070. break;
  4071. case 4:case 5:case 7:case 8:
  4072. canMove = 1;
  4073. break;
  4074. case 1: if(fatherNode->node_type&0x8 || can_move_to(childUpperLeftX-2,childUpperLeftY+1))
  4075. canMove = 1;
  4076. break;
  4077. case 2: if(fatherNode->node_type==15)
  4078. canMove = 1;
  4079. else
  4080. canMove = 0;
  4081. break;
  4082. case 3: if(fatherNode->node_type&0x1 || can_move_to(childUpperLeftX,childUpperLeftY+3))
  4083. canMove = 1;
  4084. break;
  4085. default: err_here();
  4086. break;
  4087. }
  4088. canMove = 1;
  4089. }
  4090. break;
  4091. case 3:
  4092. err_when(fatherNode->node_x!=childNode->node_x || fatherNode->node_y!=childNode->node_y+1);
  4093. if(childNode->node_type&0x4 && childNode->node_type&0x8 &&
  4094. can_move_to(childUpperLeftX,childUpperLeftY+2) && can_move_to(childUpperLeftX+1,childUpperLeftY+2))
  4095. {
  4096. switch(fatherNode->enter_direction)
  4097. {
  4098. case 7: canMove = 0; //reverse direction
  4099. break;
  4100. case 1:case 3:case 5:case 6:case 8:
  4101. canMove = 1;
  4102. break;
  4103. case 2: if(fatherNode->node_type&0x8 || can_move_to(childUpperLeftX-1,childUpperLeftY+2))
  4104. canMove = 1;
  4105. break;
  4106. case 4: if(fatherNode->node_type&0x4 || can_move_to(childUpperLeftX+2,childUpperLeftY+2))
  4107. canMove = 1;
  4108. break;
  4109. default: err_here();
  4110. break;
  4111. }
  4112. }
  4113. break;
  4114. case 4:
  4115. err_when(fatherNode->node_x!=childNode->node_x+1 || fatherNode->node_y!=childNode->node_y+1);
  4116. if(childNode->node_type&0x8 && can_move_to(childUpperLeftX+2,childUpperLeftY+1) &&
  4117. can_move_to(childUpperLeftX+1,childUpperLeftY+2) && can_move_to(childUpperLeftX+2,childUpperLeftY+2))
  4118. {
  4119. switch(fatherNode->enter_direction)
  4120. {
  4121. case 8: canMove = 0; //reverse direction
  4122. break;
  4123. case 1:case 2:case 6:case 7:
  4124. canMove = 1;
  4125. break;
  4126. case 3: if(fatherNode->node_type&0x2 || can_move_to(childUpperLeftX+1,childUpperLeftY+3))
  4127. canMove = 1;
  4128. break;
  4129. case 4: if(fatherNode->node_type==15)
  4130. canMove = 1;
  4131. else
  4132. canMove = 0;
  4133. break;
  4134. case 5: if(fatherNode->node_type&0x4 && can_move_to(childUpperLeftX+3,childUpperLeftY+1))
  4135. canMove = 1;
  4136. break;
  4137. default: err_here();
  4138. break;
  4139. }
  4140. }
  4141. break;
  4142. case 5:
  4143. err_when(fatherNode->node_x!=childNode->node_x+1 || fatherNode->node_y!=childNode->node_y);
  4144. if(childNode->node_type&0x2 && can_move_to(childUpperLeftX+2,childUpperLeftY) &&
  4145. childNode->node_type&0x8 && can_move_to(childUpperLeftX+2,childUpperLeftY+1))
  4146. {
  4147. switch(fatherNode->enter_direction)
  4148. {
  4149. case 1: canMove = 0; //reverse direction
  4150. break;
  4151. case 2:case 3:case 5:case 7:case 8:
  4152. canMove = 1;
  4153. break;
  4154. case 4: if(fatherNode->node_type&0x2 || can_move_to(childUpperLeftX+2,childUpperLeftY+2))
  4155. canMove = 1;
  4156. break;
  4157. case 6: if(fatherNode->node_type&0x8 || can_move_to(childUpperLeftX+2,childUpperLeftY-1))
  4158. canMove = 1;
  4159. break;
  4160. default: err_here();
  4161. break;
  4162. }
  4163. }
  4164. break;
  4165. case 6:
  4166. err_when(fatherNode->node_x!=childNode->node_x+1 || fatherNode->node_y!=childNode->node_y-1);
  4167. if(can_move_to(childUpperLeftX+1,childUpperLeftY-1) && can_move_to(childUpperLeftX+2,childUpperLeftY-1) &&
  4168. childNode->node_type&0x2 && can_move_to(childUpperLeftX+2,childUpperLeftY))
  4169. {
  4170. switch(fatherNode->enter_direction)
  4171. {
  4172. case 2: canMove = 0; //reverse direction
  4173. break;
  4174. case 1:case 3:case 4:case 8:
  4175. canMove = 1;
  4176. break;
  4177. case 5: if(fatherNode->node_type&0x1 || can_move_to(childUpperLeftX+3,childUpperLeftY))
  4178. canMove = 1;
  4179. break;
  4180. case 6: if(fatherNode->node_type==15)
  4181. canMove = 1;
  4182. else
  4183. canMove = 0;
  4184. break;
  4185. case 7: if(fatherNode->node_type&0x8 || can_move_to(childUpperLeftX+1,childUpperLeftY-2))
  4186. canMove = 1;
  4187. break;
  4188. default: err_here();
  4189. break;
  4190. }
  4191. }
  4192. break;
  4193. case 7:
  4194. err_when(fatherNode->node_x!=childNode->node_x || fatherNode->node_y!=childNode->node_y-1);
  4195. if(can_move_to(childUpperLeftX,childUpperLeftY-1) && can_move_to(childUpperLeftX+1,childUpperLeftY-1) &&
  4196. childNode->node_type&0x1 && childNode->node_type&0x2)
  4197. {
  4198. switch(fatherNode->enter_direction)
  4199. {
  4200. case 3: canMove = 0; //reverse direction
  4201. break;
  4202. case 1:case 2:case 4:case 5:case 7:
  4203. canMove = 1;
  4204. break;
  4205. case 6: if(fatherNode->node_type&0x1 || can_move_to(childUpperLeftX+2,childUpperLeftY-1))
  4206. canMove = 1;
  4207. break;
  4208. case 8: if(fatherNode->node_type&0x2 || can_move_to(childUpperLeftX-1,childUpperLeftY-1))
  4209. canMove = 1;
  4210. break;
  4211. default: err_here();
  4212. break;
  4213. }
  4214. }
  4215. break;
  4216. case 8:
  4217. err_when(fatherNode->node_x!=childNode->node_x-1 || fatherNode->node_y!=childNode->node_y-1);
  4218. if(can_move_to(childUpperLeftX-1,childUpperLeftY-1) && can_move_to(childUpperLeftX,childUpperLeftY-1) &&
  4219. can_move_to(childUpperLeftX-1,childUpperLeftY) && childNode->node_type&0x1)//can_move_to(childUpperLeftX,childUpperLeftY))
  4220. {
  4221. switch(fatherNode->enter_direction)
  4222. {
  4223. case 4: canMove = 0; //reverse direction
  4224. break;
  4225. case 2:case 3:case 5:case 6:
  4226. canMove = 1;
  4227. break;
  4228. case 1: if(fatherNode->node_type&0x2 || can_move_to(childUpperLeftX-2,childUpperLeftY))
  4229. canMove = 1;
  4230. break;
  4231. case 8: if(fatherNode->node_type==15)
  4232. canMove = 1;
  4233. else
  4234. canMove = 0;
  4235. break;
  4236. case 7: if(fatherNode->node_type&0x4 || can_move_to(childUpperLeftX,childUpperLeftY-2))
  4237. canMove = 1;
  4238. break;
  4239. default: err_here();
  4240. break;
  4241. }
  4242. }
  4243. break;
  4244. default: err_here();
  4245. break;
  4246. }
  4247. if(canMove)
  4248. {
  4249. exitDirection = (childEnterDirection+3)%8+1;
  4250. if(fatherNode->enter_direction > exitDirection)
  4251. {
  4252. if((fatherNode->enter_direction==8 && (exitDirection==1 || exitDirection==2)) ||
  4253. (fatherNode->enter_direction==7 && exitDirection==1))
  4254. testResult = exitDirection + 8 - fatherNode->enter_direction;
  4255. else
  4256. testResult = fatherNode->enter_direction - exitDirection;
  4257. }
  4258. else
  4259. {
  4260. if((exitDirection==8 && (fatherNode->enter_direction==1 || fatherNode->enter_direction==2)) ||
  4261. (exitDirection==7 && fatherNode->enter_direction==1))
  4262. testResult = fatherNode->enter_direction + 8 - exitDirection;
  4263. else
  4264. testResult = exitDirection - fatherNode->enter_direction;
  4265. }
  4266. if((exitDirection%2 && testResult<=2) || (!exitDirection%2 && testResult<=1))
  4267. cost = 1;
  4268. else
  4269. cost = 2;
  4270. err_when(cost>2 || cost<1);
  4271. if(g+cost < childNode->node_g) // second checking, mainly for cost = 2;
  4272. {
  4273. char canPropagateDown=1;
  4274. if(childNode->node_type!=15 && childEnterDirection%2==0)
  4275. {
  4276. NodeS2* oldChildNode;
  4277. short xShift1 = x_shift_array[childEnterDirection-1];
  4278. short yShift1 = y_shift_array[childEnterDirection-1];
  4279. short xShift2, yShift2, xShift3, yShift3;
  4280. if(childEnterDirection>1)
  4281. {
  4282. xShift2 = x_shift_array[childEnterDirection-2];
  4283. yShift2 = y_shift_array[childEnterDirection-2];
  4284. }
  4285. else
  4286. {
  4287. xShift2 = x_shift_array[7];
  4288. yShift2 = y_shift_array[7];
  4289. }
  4290. if(childEnterDirection<8)
  4291. {
  4292. xShift3 = x_shift_array[childEnterDirection];
  4293. yShift3 = y_shift_array[childEnterDirection];
  4294. }
  4295. else
  4296. {
  4297. xShift3 = x_shift_array[0];
  4298. yShift3 = y_shift_array[0];
  4299. }
  4300. for(c=0; c<MAX_CHILD_NODE && childNode->child_node[c]; c++) // for each child of oldNode
  4301. {
  4302. oldChildNode = childNode->child_node[c];
  4303. if(childNode->parent_node != oldChildNode)
  4304. {
  4305. switch(childEnterDirection)
  4306. {
  4307. case 2:
  4308. if(oldChildNode->enter_direction==childEnterDirection &&
  4309. oldChildNode->node_x-childNode->node_x==xShift1 &&
  4310. oldChildNode->node_y-childNode->node_y==yShift1)
  4311. canPropagateDown = 0;
  4312. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  4313. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4314. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4315. !can_move_to(childUpperLeftX,childUpperLeftY)&&
  4316. !can_move_to(childUpperLeftX+1,childUpperLeftY+2))
  4317. canPropagateDown = 0;
  4318. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  4319. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4320. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4321. !can_move_to(childUpperLeftX+1,childUpperLeftY+1) &&
  4322. !can_move_to(childUpperLeftX-1,childUpperLeftY))
  4323. canPropagateDown = 0;
  4324. break;
  4325. case 4:
  4326. if(oldChildNode->enter_direction==childEnterDirection &&
  4327. oldChildNode->node_x-childNode->node_x==xShift1 &&
  4328. oldChildNode->node_y-childNode->node_y==yShift1)
  4329. canPropagateDown = 0;
  4330. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  4331. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4332. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4333. !can_move_to(childUpperLeftX,childUpperLeftY+1)&&
  4334. !can_move_to(childUpperLeftX+2,childUpperLeftY))
  4335. canPropagateDown = 0;
  4336. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  4337. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4338. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4339. !can_move_to(childUpperLeftX,childUpperLeftY+2) &&
  4340. !can_move_to(childUpperLeftX+1,childUpperLeftY))
  4341. canPropagateDown = 0;
  4342. break;
  4343. case 6:
  4344. if(oldChildNode->enter_direction==childEnterDirection &&
  4345. oldChildNode->node_x-childNode->node_x==xShift1 &&
  4346. oldChildNode->node_y-childNode->node_y==yShift1)
  4347. canPropagateDown = 0;
  4348. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  4349. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4350. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4351. !can_move_to(childUpperLeftX,childUpperLeftY-1)&&
  4352. !can_move_to(childUpperLeftX+1,childUpperLeftY+1))
  4353. canPropagateDown = 0;
  4354. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  4355. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4356. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4357. !can_move_to(childUpperLeftX,childUpperLeftY) &&
  4358. !can_move_to(childUpperLeftX+2,childUpperLeftY+1))
  4359. canPropagateDown = 0;
  4360. break;
  4361. case 8:
  4362. if(oldChildNode->enter_direction==childEnterDirection &&
  4363. oldChildNode->node_x-childNode->node_x==xShift1 &&
  4364. oldChildNode->node_y-childNode->node_y==yShift1)
  4365. canPropagateDown = 0;
  4366. if(oldChildNode->enter_direction==childEnterDirection-1 &&
  4367. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4368. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4369. !can_move_to(childUpperLeftX-1,childUpperLeftY+1)&&
  4370. !can_move_to(childUpperLeftX+1,childUpperLeftY))
  4371. canPropagateDown = 0;
  4372. if(oldChildNode->enter_direction==childEnterDirection+1 &&
  4373. oldChildNode->node_x-childNode->node_x==xShift2 &&
  4374. oldChildNode->node_x-childNode->node_y==yShift2 &&
  4375. !can_move_to(childUpperLeftX+1,childUpperLeftY-1) &&
  4376. !can_move_to(childUpperLeftX,childUpperLeftY+1))
  4377. canPropagateDown = 0;
  4378. break;
  4379. default: err_here();
  4380. break;
  4381. }
  4382. }
  4383. }
  4384. }
  4385. if(canPropagateDown)
  4386. {
  4387. childNode->node_g = g+cost;
  4388. childNode->node_f = childNode->node_g+childNode->node_h;
  4389. childNode->parent_node = fatherNode;
  4390. childNode->enter_direction = childEnterDirection;
  4391. stack_push(childNode);
  4392. }
  4393. }
  4394. }
  4395. }
  4396. }
  4397. }
  4398. }
  4399. //------- End of function NodeS2::propagate_down -------//
  4400. //-------- Begin of static function stack_push ---------//
  4401. static void stack_push(NodeS2 *nodePtr)
  4402. {
  4403. stack_array[cur_stack_pos_s2++] = nodePtr;
  4404. err_when( cur_stack_pos_s2 >= MAX_STACK_NUM );
  4405. }
  4406. //--------- End of static function stack_push ---------//
  4407. //-------- Begin of static function stack_pop ---------//
  4408. static NodeS2* stack_pop()
  4409. {
  4410. return stack_array[--cur_stack_pos_s2];
  4411. }
  4412. //--------- End of static function stack_pop ---------//