Move.cpp 208 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451
  1. //***************************************************************************
  2. //
  3. // move.cp - This file contains the Movement\Pathfinding routines
  4. //
  5. //---------------------------------------------------------------------------//
  6. // Copyright (C) Microsoft Corporation. All rights reserved. //
  7. //===========================================================================//
  8. //--------------
  9. // Include Files
  10. #ifndef MCLIB_H
  11. #include "mclib.h"
  12. #endif
  13. #ifndef MOVE_H
  14. #include "move.h"
  15. #endif
  16. #define MOVELOG
  17. #ifdef MOVELOG
  18. #ifndef GAMELOG_H
  19. #include "gamelog.h"
  20. #endif
  21. #endif
  22. //***************************************************************************
  23. #define USE_SEPARATE_WATER_MAPS FALSE
  24. #ifndef TEAM_H
  25. typedef enum {
  26. RELATION_FRIENDLY,
  27. RELATION_NEUTRAL,
  28. RELATION_ENEMY,
  29. NUM_RELATIONS
  30. } Relation;
  31. #define MAX_TEAMS 8
  32. #endif
  33. //------------
  34. // EXTERN vars
  35. extern void memclear(void *Dest,int Length);
  36. extern UserHeapPtr systemHeap;
  37. extern float metersPerWorldUnit;
  38. extern char* ExceptionGameMsg;
  39. extern char ChunkDebugMsg[5120];
  40. long GlobalMap::minRow = 0;
  41. long GlobalMap::maxRow = 0;
  42. long GlobalMap::minCol = 0;
  43. long GlobalMap::maxCol = 0;
  44. GameLogPtr GlobalMap::log = NULL;
  45. bool GlobalMap::logEnabled = false;
  46. //------------
  47. // GLOBAL vars
  48. bool ZeroHPrime = false;
  49. bool CullPathAreas = false;
  50. const float LOSincrement = 0.33f; // based upon cell size
  51. const float LOFincrement = 0.33f; // based upon cell size
  52. const float LOSensorIncrement = 2.0; // based upon cell size (big, since we care only about tiles)
  53. bool debugMoveMap = false;
  54. bool ClearBridgeTiles = false;
  55. long RamObjectWID = 0;
  56. PriorityQueuePtr openList = NULL;
  57. bool JumpOnBlocked = false;
  58. bool FindingEscapePath = false;
  59. bool BlockWallTiles = true;
  60. MissionMapPtr GameMap = NULL;
  61. GlobalMapPtr GlobalMoveMap[3] = {NULL, NULL, NULL};
  62. extern float worldUnitsPerMeter; //Assumes 90 pixel mechs
  63. extern float metersPerWorldUnit; //Assumes 90 pixel mechs
  64. long DebugMovePathType = 0;
  65. long SimpleMovePathRange = 21;
  66. char reverseDir[NUM_DIRECTIONS] = {4, 5, 6, 7, 0, 1, 2, 3};
  67. char rowShift[NUM_DIRECTIONS] = {-1, -1, 0, 1, 1, 1, 0, -1};
  68. char colShift[NUM_DIRECTIONS] = {0, 1, 1, 1, 0, -1, -1, -1};
  69. #define NUM_CELL_OFFSETS 128
  70. long cellShift[NUM_CELL_OFFSETS * 2] = {
  71. -1, 0,
  72. -1, 1,
  73. 0, 1,
  74. 1, 1,
  75. 1, 0,
  76. 1, -1,
  77. 0, -1,
  78. -1, -1,
  79. -2, 0,
  80. -2, 2,
  81. 0, 2,
  82. 2, 2,
  83. 2, 0,
  84. 2, -2,
  85. 0, -2,
  86. -2, -2,
  87. -3, 0,
  88. -3, 3,
  89. 0, 3,
  90. 3, 3,
  91. 3, 0,
  92. 3, -3,
  93. 0, -3,
  94. -3, -3,
  95. -3, 2,
  96. -2, 3,
  97. 2, 3,
  98. 3, 2,
  99. 3, -2,
  100. 2, -3,
  101. -2, -3,
  102. -3, -2,
  103. -4, 0,
  104. -4, 4,
  105. 0, 4,
  106. 4, 4,
  107. 4, 0,
  108. 4, -4,
  109. 0, -4,
  110. -4, -4,
  111. -5, 0,
  112. -5, 5,
  113. 0, 5,
  114. 5, 5,
  115. 5, 0,
  116. 5, -5,
  117. 0, -5,
  118. -5, -5,
  119. -6, 0,
  120. -6, 6,
  121. 0, 6,
  122. 6, 6,
  123. 6, 0,
  124. 6, -6,
  125. 0, -6,
  126. -6, -6,
  127. -7, 0,
  128. -7, 7,
  129. 0, 7,
  130. 7, 7,
  131. 7, 0,
  132. 7, -7,
  133. 0, -7,
  134. -7, -7,
  135. -8, 0,
  136. -8, 8,
  137. 0, 8,
  138. 8, 8,
  139. 8, 0,
  140. 8, -8,
  141. 0, -8,
  142. -8, -8,
  143. -9, 0,
  144. -9, 9,
  145. 0, 9,
  146. 9, 9,
  147. 9, 0,
  148. 9, -9,
  149. 0, -9,
  150. -9, -9,
  151. -10, 0,
  152. -10, 10,
  153. 0, 10,
  154. 10, 10,
  155. 10, 0,
  156. 10, -10,
  157. 0, -10,
  158. -10, -10,
  159. -11, 0,
  160. -11, 11,
  161. 0, 11,
  162. 11, 11,
  163. 11, 0,
  164. 11, -11,
  165. 0, -11,
  166. -11, -11,
  167. -12, 0,
  168. -12, 12,
  169. 0, 12,
  170. 12, 12,
  171. 12, 0,
  172. 12, -12,
  173. 0, -12,
  174. -12, -12,
  175. -13, 0,
  176. -13, 13,
  177. 0, 13,
  178. 13, 13,
  179. 13, 0,
  180. 13, -13,
  181. 0, -13,
  182. -13, -13,
  183. -14, 0,
  184. -14, 14,
  185. 0, 14,
  186. 14, 14,
  187. 14, 0,
  188. 14, -14,
  189. 0, -14,
  190. -14, -14,
  191. -15, 0,
  192. -15, 15,
  193. 0, 15,
  194. 15, 15,
  195. 15, 0,
  196. 15, -15,
  197. 0, -15,
  198. -15, -15
  199. };
  200. float cellShiftDistance[NUM_CELL_OFFSETS];
  201. char reverseShift[NUM_CELL_OFFSETS] = {
  202. 4, 5, 6, 7, 0, 1, 2, 3,
  203. 12, 13, 14, 15, 8, 9, 10, 11,
  204. 20, 21, 22, 23, 16, 17, 18, 19,
  205. 28, 29, 30, 31, 24, 25, 26, 27,
  206. 36, 37, 38, 39, 32, 33, 34, 35,
  207. 44, 45, 46, 47, 40, 41, 42, 43,
  208. 52, 53, 54, 55, 48, 49, 50, 51,
  209. 60, 61, 62, 63, 56, 57, 58, 59,
  210. 68, 69, 70, 71, 64, 65, 66, 67,
  211. 76, 77, 78, 79, 72, 73, 74, 75,
  212. 84, 85, 86, 87, 80, 81, 82, 83,
  213. 92, 93, 94, 95, 88, 89, 90, 91,
  214. 100, 101, 102, 103, 96, 97, 98, 99,
  215. 108,109,110,111,104,105,106,107,
  216. 116,117,118,119,112,113,114,115,
  217. 124,125,126,127,120,121,122,123
  218. };
  219. long tileMulMAPCELL_DIM[MAX_MAP_CELL_WIDTH];
  220. float MapCellDiagonal = 0.0;
  221. float HalfMapCell = 0.0;
  222. float VerticesMapSideDivTwo = 0.0;
  223. float MetersMapSideDivTwo = 0.0;
  224. bool IsDiagonalStep[NUM_CELL_OFFSETS] = {
  225. false, true, false, true, false, true, false, true,
  226. false, false, false, false, false, false, false, false,
  227. false, false, false, false, false, false, false, false,
  228. false, false, false, false, false, false, false, false,
  229. false, false, false, false, false, false, false, false,
  230. false, false, false, false, false, false, false, false,
  231. false, false, false, false, false, false, false, false,
  232. false, false, false, false, false, false, false, false,
  233. false, false, false, false, false, false, false, false,
  234. false, false, false, false, false, false, false, false,
  235. false, false, false, false, false, false, false, false,
  236. false, false, false, false, false, false, false, false,
  237. false, false, false, false, false, false, false, false,
  238. false, false, false, false, false, false, false, false,
  239. false, false, false, false, false, false, false, false,
  240. false, false, false, false, false, false, false, false
  241. };
  242. long StepAdjDir[9] = {-1, 0, 2, 2, 4, 4, 6, 6, 0};
  243. long adjTile[4][2] = {
  244. {-1, 0},
  245. {0, 1},
  246. {1, 0},
  247. {0, -1}
  248. };
  249. inline float agsqrt( float _a, float _b )
  250. {
  251. return sqrt(_a*_a + _b*_b);
  252. }
  253. //-------------------------------------------------
  254. // Only pattern 2 is used now.
  255. char mineLayout[4][MAPCELL_DIM * MAPCELL_DIM] = {
  256. {0,0,0,0,0,0,0,0,0},
  257. {0,0,0,0,0,0,0,0,0},
  258. {1,0,1,0,1,0,1,0,1},
  259. {0,0,0,0,0,0,0,0,0}
  260. };
  261. char TeamRelations[MAX_TEAMS][MAX_TEAMS] = {
  262. {0, 2, RELATION_NEUTRAL, 2, 2, 2, 2, 2},
  263. {2, 0, 2, 2, 2, 2, 2, 2},
  264. {RELATION_NEUTRAL, 2, 0, 2, 2, 2, 2, 2},
  265. {2, 2, 2, 0, 2, 2, 2, 2},
  266. {2, 2, 2, 2, 0, 2, 2, 2},
  267. {2, 2, 2, 2, 2, 0, 2, 2},
  268. {2, 2, 2, 2, 2, 2, 0, 2},
  269. {2, 2, 2, 2, 2, 2, 2, 0}
  270. };
  271. bool GoalIsDoor = false;
  272. long numNodesVisited = 0;
  273. long topOpenNodes = 0;
  274. long MaxHPrime = 1000;
  275. bool PreserveMapTiles = false;
  276. MoveMapPtr PathFindMap[2] = {NULL, NULL};
  277. float MoveMap::distanceFloat[DISTANCE_TABLE_DIM][DISTANCE_TABLE_DIM];
  278. long MoveMap::distanceLong[DISTANCE_TABLE_DIM][DISTANCE_TABLE_DIM];
  279. long MoveMap::forestCost = 50;
  280. #ifdef LAB_ONLY
  281. __int64 MCTimeCalcPath1Update = 0;
  282. __int64 MCTimeCalcPath2Update = 0;
  283. __int64 MCTimeCalcPath3Update = 0;
  284. __int64 MCTimeCalcPath4Update = 0;
  285. __int64 MCTimeCalcPath5Update = 0;
  286. #endif
  287. //***************************************************************************
  288. // MISC routines
  289. //***************************************************************************
  290. inline void calcAdjNode (long& r, long& c, long direction) {
  291. r += rowShift[direction];
  292. c += colShift[direction];
  293. }
  294. //---------------------------------------------------------------------------
  295. inline bool inMapBounds (long r, long c, long mapHeight, long mapWidth) {
  296. return((r >= 0) && (r < mapHeight) && (c >= 0) && (c < mapWidth));
  297. }
  298. //---------------------------------------------------------------------------
  299. Stuff::Vector3D relativePositionToPoint (Stuff::Vector3D point, float angle, float distance, unsigned long flags) {
  300. //--------------------------------------------------------
  301. // Note that the angle should be -180 <= angle <= 180, and
  302. // the distance is in meters...
  303. Stuff::Vector3D curPos;
  304. curPos.x = point.x;
  305. curPos.y = point.y;
  306. curPos.z = 0.0;
  307. distance *= -worldUnitsPerMeter;
  308. Stuff::Vector3D shiftVect;
  309. shiftVect.x = 0.0;
  310. shiftVect.y = 1.0;
  311. shiftVect.z = 0.0;
  312. Rotate(shiftVect,angle);
  313. shiftVect *= distance;
  314. Stuff::Vector3D relPos;
  315. relPos.Zero();
  316. relPos.Add(curPos,shiftVect);
  317. Stuff::Vector3D start2d;
  318. Stuff::Vector3D goal2d;
  319. Stuff::Vector3D deltaVector;
  320. if (flags & RELPOS_FLAG_PASSABLE_START) {
  321. start2d = curPos;
  322. goal2d = relPos;
  323. }
  324. else {
  325. start2d = relPos;
  326. goal2d = curPos;
  327. }
  328. deltaVector.Subtract(goal2d,start2d);
  329. //-------------------------------------------------------------
  330. // First, we need to calc the delta vector--how much we extend
  331. // the ray everytime we check the map cell for clear placement.
  332. deltaVector.Normalize(deltaVector);
  333. deltaVector *= Terrain::halfWorldUnitsPerCell;
  334. if (deltaVector.GetLength() == 0.0)
  335. return(curPos);
  336. //-------------------------------------------------
  337. // Determine the max length the ray must be cast...
  338. float maxLength = distance_from(goal2d,start2d);
  339. //------------------------------------------------------------
  340. // We'll start at the target, and if it's blocked, we'll move
  341. // toward our start location, looking for the first valid/open
  342. // cell...
  343. Stuff::Vector3D curPoint = start2d;
  344. Stuff::Vector3D curRay;
  345. curRay.Zero();
  346. float rayLength = 0.0;
  347. long cellR, cellC;
  348. Stuff::Vector3D curPoint3d(curPoint.x, curPoint.y, 0.0);
  349. land->worldToCell(curPoint3d, cellR, cellC);
  350. unsigned long cellClear = GameMap->getPassable(cellR, cellC);
  351. Stuff::Vector3D lastGoodPoint = curPoint;
  352. if (flags & RELPOS_FLAG_PASSABLE_START)
  353. while (cellClear && (rayLength < maxLength)) {
  354. lastGoodPoint = curPoint;
  355. curPoint += deltaVector;
  356. curRay.Subtract(curPoint,start2d);
  357. rayLength = curRay.GetLength();
  358. curPoint3d.x = curPoint.x;
  359. curPoint3d.x = curPoint.y;
  360. curPoint3d.z = 0.0;
  361. land->worldToCell(curPoint3d, cellR, cellC);
  362. cellClear = GameMap->getPassable(cellR, cellC);
  363. }
  364. else
  365. while (!cellClear && (rayLength < maxLength)) {
  366. lastGoodPoint = curPoint;
  367. curPoint += deltaVector;
  368. curRay.Subtract(curPoint,start2d);
  369. rayLength = curRay.GetLength();
  370. curPoint3d.x = curPoint.x;
  371. curPoint3d.x = curPoint.y;
  372. curPoint3d.z = 0.0;
  373. land->worldToCell(curPoint3d, cellR, cellC);
  374. cellClear = GameMap->getPassable(cellR, cellC);
  375. }
  376. //----------------------------------------
  377. // lastGoodPoint MUST be on Map or BOOM!!
  378. // back on map if necessary
  379. float maxMap = Terrain::worldUnitsMapSide / 2.0;
  380. float safety = maxMap - Terrain::worldUnitsPerVertex;
  381. bool offMap = false;
  382. if (lastGoodPoint.x < -safety)
  383. {
  384. lastGoodPoint.x = -safety;
  385. offMap = true;
  386. }
  387. if (lastGoodPoint.x > safety)
  388. {
  389. lastGoodPoint.x = safety;
  390. offMap = true;
  391. }
  392. if (lastGoodPoint.y < -safety)
  393. {
  394. lastGoodPoint.y = -safety;
  395. offMap = true;
  396. }
  397. if (lastGoodPoint.y > safety)
  398. {
  399. lastGoodPoint.y = safety;
  400. offMap = true;
  401. }
  402. curPoint3d.x = lastGoodPoint.x;
  403. curPoint3d.x = lastGoodPoint.y;
  404. curPoint3d.z = 0.0;
  405. curPoint3d.z = land->getTerrainElevation(curPoint3d);
  406. return(curPoint3d);
  407. }
  408. //***************************************************************************
  409. // MISSION MAP class (new Game Map class)
  410. //***************************************************************************
  411. //#define DEBUG_GLOBALMAP_BUILD
  412. void MOVE_init (long moveRange) {
  413. if (PathFindMap[SECTOR_PATHMAP])
  414. Fatal(0, " MOVE_Init: Already called this! ");
  415. PathFindMap[SECTOR_PATHMAP] = new MoveMap;
  416. if (!PathFindMap[SECTOR_PATHMAP])
  417. Fatal(0, " MOVE_Init: Cannot initialize PathFindMap ");
  418. PathFindMap[SECTOR_PATHMAP]->init(SECTOR_DIM*2, SECTOR_DIM*2);
  419. PathFindMap[SIMPLE_PATHMAP] = new MoveMap;
  420. if (!PathFindMap[SIMPLE_PATHMAP])
  421. Fatal(0, " MOVE_Init: Cannot initialize PathFindMap ");
  422. SimpleMovePathRange = moveRange;
  423. if (SimpleMovePathRange <= 20)
  424. Fatal(0, " MOVE_Init: MoveRange TOO SMALL. Go see Glenn. ");
  425. PathFindMap[SIMPLE_PATHMAP]->init(SimpleMovePathRange * 2 + 1, SimpleMovePathRange * 2 + 1);
  426. }
  427. //---------------------------------------------------------------------------
  428. bool EditorSave = false;
  429. long tempNumSpecialAreas = 0;
  430. GameObjectFootPrint* tempSpecialAreaFootPrints = NULL;
  431. void MOVE_buildData (long height, long width, MissionMapCellInfo* mapData, long numSpecialAreas, GameObjectFootPrint* specialAreaFootPrints) {
  432. EditorSave = true;
  433. if (GameMap)
  434. delete GameMap;
  435. GameMap = new MissionMap;
  436. gosASSERT(GameMap != NULL);
  437. if (mapData)
  438. GameMap->init(height, width, 0, mapData);
  439. else
  440. GameMap->init( height, width );
  441. long numOffMap = 0;
  442. for (long r = 0; r < height; r++)
  443. for (long c = 0; c < width; c++) {
  444. Stuff::Vector3D worldPos;
  445. land->cellToWorld(r, c, worldPos);
  446. bool set = (land->IsEditorSelectTerrainPosition(worldPos) && !land->IsGameSelectTerrainPosition(worldPos));
  447. GameMap->setOffMap(r, c, set);
  448. if (set)
  449. numOffMap++;
  450. }
  451. if (tempSpecialAreaFootPrints) {
  452. systemHeap->Free(tempSpecialAreaFootPrints);
  453. tempSpecialAreaFootPrints = NULL;
  454. }
  455. tempNumSpecialAreas = numSpecialAreas;
  456. if (tempNumSpecialAreas) {
  457. tempSpecialAreaFootPrints = (GameObjectFootPrint*)systemHeap->Malloc(sizeof(GameObjectFootPrint) * tempNumSpecialAreas);
  458. memcpy(tempSpecialAreaFootPrints, specialAreaFootPrints, sizeof(GameObjectFootPrint) * tempNumSpecialAreas);
  459. }
  460. if (!PathFindMap[SECTOR_PATHMAP])
  461. Fatal(0, " MOVE_BuildData: Must call MOVE_Init()! ");
  462. if (GlobalMoveMap[0])
  463. {
  464. delete GlobalMoveMap[0];
  465. GlobalMoveMap[0] = NULL;
  466. }
  467. if (GlobalMoveMap[1])
  468. {
  469. delete GlobalMoveMap[1];
  470. GlobalMoveMap[1] = NULL;
  471. }
  472. if (GlobalMoveMap[2])
  473. {
  474. delete GlobalMoveMap[2];
  475. GlobalMoveMap[2] = NULL;
  476. }
  477. GlobalMoveMap[0] = new GlobalMap;
  478. if (!GlobalMoveMap[0])
  479. Fatal(0, " MOVE_Init: Cannot initialize GlobalMoveMap ");
  480. GlobalMoveMap[0]->build(mapData);
  481. GlobalMoveMap[1] = new GlobalMap;
  482. if (!GlobalMoveMap[1])
  483. Fatal(0, " MOVE_Init: Cannot initialize GlobalMoveMap ");
  484. GlobalMoveMap[1]->hover = true;
  485. GlobalMoveMap[1]->build(mapData);
  486. GlobalMoveMap[2] = new GlobalMap;
  487. if (!GlobalMoveMap[2])
  488. Fatal(0, " MOVE_Init: Cannot initialize HeliGlobalMoveMap ");
  489. GlobalMoveMap[2]->blank = true;
  490. GlobalMoveMap[2]->build(NULL);
  491. //-----------------------
  492. // Just some debugging...
  493. //for (r = 0; r < height; r++)
  494. // for (long c = 0; c < width; c++)
  495. // if (GameMap->getOffMap(r, c))
  496. // numOffMap++;
  497. //-------------------------------------------------------------------------------------
  498. // Since we use this bitfield during the globalmap build for something else, we can now
  499. // reset it...
  500. for (long row = 0; row < GameMap->height; row++)
  501. for (long col = 0; col < GameMap->width; col++)
  502. for (long i = 0; i < NUM_MOVE_LEVELS; i++)
  503. GameMap->setPathlock(i, row, col, false);
  504. EditorSave = false;
  505. }
  506. //---------------------------------------------------------------------------
  507. long MOVE_saveData (PacketFile* packetFile, long whichPacket) {
  508. if (!GameMap)
  509. Fatal(0, " MOVE_SaveData: Cannot initialize GameMap ");
  510. if (!GlobalMoveMap[0])
  511. Fatal(0, " MOVE_SaveData: Cannot initialize GlobalMoveMap ");
  512. if (!packetFile)
  513. return(GameMap->write(NULL) + GlobalMoveMap[0]->write(NULL) + GlobalMoveMap[1]->write(NULL) + GlobalMoveMap[2]->write(NULL) + 2);
  514. //-----------------------
  515. // Just some debugging...
  516. //long numOffMap = 0;
  517. //for (long r = 0; r < GameMap->height; r++)
  518. // for (long c = 0; c < GameMap->width; c++)
  519. // if (GameMap->getOffMap(r, c))
  520. // numOffMap++;
  521. long numMissionMapPackets = GameMap->write(packetFile, whichPacket);
  522. long numGlobalMap0Packets = GlobalMoveMap[0]->write(packetFile, whichPacket + numMissionMapPackets);
  523. long numGlobalMap1Packets = GlobalMoveMap[1]->write(packetFile, whichPacket + numMissionMapPackets + numGlobalMap0Packets);
  524. long numGlobalMap2Packets = GlobalMoveMap[2]->write(packetFile, whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets);
  525. long result = packetFile->writePacket(whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets, (unsigned char*)&tempNumSpecialAreas, sizeof(long));
  526. if (result <= 0)
  527. Fatal(result, " MOVE_saveData: Unable to write num special area ");
  528. //-------------------------------------------------------------------------------------------------
  529. // If there are no special areas, we just write the specialArea count as a filler for the packet...
  530. if (tempNumSpecialAreas == 0)
  531. result = packetFile->writePacket(whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets + 1, (unsigned char*)&tempNumSpecialAreas, sizeof(long));
  532. else
  533. result = packetFile->writePacket(whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets + 1, (unsigned char*)tempSpecialAreaFootPrints, sizeof(GameObjectFootPrint) * tempNumSpecialAreas);
  534. if (result <= 0)
  535. Fatal(result, " MOVE_saveData: Unable to write special area footprints ");
  536. return(numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets + 2);
  537. }
  538. //---------------------------------------------------------------------------
  539. long MOVE_readData (PacketFile* packetFile, long whichPacket) {
  540. if (GameMap)
  541. delete GameMap;
  542. GameMap = new MissionMap;
  543. gosASSERT(GameMap != NULL);
  544. long numMissionMapPackets = GameMap->init(packetFile, whichPacket);
  545. //-----------------------
  546. // Just some debugging...
  547. //long numOffMap[2] = {0,0};
  548. //for (long r = 0; r < GameMap->height; r++)
  549. // for (long c = 0; c < GameMap->width; c++) {
  550. // if (GameMap->getOffMap(r, c))
  551. // numOffMap[0]++;
  552. // Stuff::Vector3D worldPos;
  553. // land->cellToWorld(r, c, worldPos);
  554. // bool set = (land->IsEditorSelectTerrainPosition(worldPos) && !land->IsGameSelectTerrainPosition(worldPos));
  555. // if (set)
  556. // numOffMap[1]++;
  557. // if (set && !GameMap->getOffMap(r, c))
  558. // set = false;
  559. // }
  560. if (!PathFindMap[SECTOR_PATHMAP])
  561. Fatal(0, " MOVE_BuildData: Must call MOVE_Init()! ");
  562. if (GlobalMoveMap[0])
  563. delete GlobalMoveMap[0];
  564. GlobalMoveMap[0] = new GlobalMap;
  565. if (!GlobalMoveMap[0])
  566. Fatal(0, " MOVE_Init: Cannot initialize GlobalMoveMap0 ");
  567. long numGlobalMap0Packets = GlobalMoveMap[0]->init(packetFile, whichPacket + numMissionMapPackets);
  568. long numGlobalMap1Packets = 0;
  569. long numGlobalMap2Packets = 0;
  570. if (!GlobalMoveMap[0]->badLoad) {
  571. //---------------------------------
  572. // Let's read the hover move map...
  573. if (GlobalMoveMap[1]) {
  574. delete GlobalMoveMap[1];
  575. GlobalMoveMap[1] = NULL;
  576. }
  577. GlobalMoveMap[1] = new GlobalMap;
  578. if (!GlobalMoveMap[1])
  579. Fatal(0, " MOVE_Init: Cannot initialize GlobalMoveMap1 ");
  580. GlobalMoveMap[1]->hover = true;
  581. numGlobalMap1Packets = GlobalMoveMap[1]->init(packetFile, whichPacket + numMissionMapPackets + numGlobalMap0Packets);
  582. //--------------------------------------
  583. // Let's read the helicoptor move map...
  584. if (GlobalMoveMap[2]) {
  585. delete GlobalMoveMap[2];
  586. GlobalMoveMap[2] = NULL;
  587. }
  588. GlobalMoveMap[2] = new GlobalMap;
  589. if (!GlobalMoveMap[2])
  590. Fatal(0, " MOVE_Init: Cannot initialize GlobalMoveMap2 ");
  591. GlobalMoveMap[2]->blank = true;
  592. numGlobalMap2Packets = GlobalMoveMap[2]->init(packetFile, whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets);
  593. //------------------------------------------------------------------
  594. // Delete the pathExistsTable for this one, since we don't use it...
  595. systemHeap->Free(GlobalMoveMap[2]->pathExistsTable);
  596. GlobalMoveMap[2]->pathExistsTable = NULL;
  597. long numBytes = packetFile->readPacket(whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets, (unsigned char*)&tempNumSpecialAreas);
  598. if (numBytes <= 0)
  599. Fatal(numBytes, " MOVE_readData: Unable to read num special areas ");
  600. if (tempSpecialAreaFootPrints) {
  601. systemHeap->Free(tempSpecialAreaFootPrints);
  602. tempSpecialAreaFootPrints = NULL;
  603. }
  604. if (tempNumSpecialAreas > 0) {
  605. tempSpecialAreaFootPrints = (GameObjectFootPrint*)systemHeap->Malloc(sizeof(GameObjectFootPrint) * tempNumSpecialAreas);
  606. numBytes = packetFile->readPacket(whichPacket + numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets + 1, (unsigned char*)tempSpecialAreaFootPrints);
  607. if (numBytes <= 0)
  608. Fatal(numBytes, " MOVE_readData: Unable to read num special areas ");
  609. }
  610. }
  611. //-----------------------
  612. // Just some debugging...
  613. //numOffMap[0] = 0;
  614. //for (r = 0; r < GameMap->height; r++)
  615. // for (long c = 0; c < GameMap->width; c++)
  616. // if (GameMap->getOffMap(r, c))
  617. // numOffMap[0]++;
  618. return(numMissionMapPackets + numGlobalMap0Packets + numGlobalMap1Packets + numGlobalMap2Packets + 2);
  619. }
  620. //---------------------------------------------------------------------------
  621. void MOVE_cleanup (void) {
  622. if (GameMap) {
  623. delete GameMap;
  624. GameMap = NULL;
  625. }
  626. if (GlobalMoveMap[0])
  627. {
  628. delete GlobalMoveMap[0];
  629. GlobalMoveMap[0] = NULL;
  630. }
  631. if (GlobalMoveMap[1])
  632. {
  633. delete GlobalMoveMap[1];
  634. GlobalMoveMap[1] = NULL;
  635. }
  636. if (GlobalMoveMap[2])
  637. {
  638. delete GlobalMoveMap[2];
  639. GlobalMoveMap[2] = NULL;
  640. }
  641. if (PathFindMap[SECTOR_PATHMAP]) {
  642. delete PathFindMap[SECTOR_PATHMAP];
  643. PathFindMap[SECTOR_PATHMAP] = NULL;
  644. }
  645. if (PathFindMap[SIMPLE_PATHMAP]) {
  646. delete PathFindMap[SIMPLE_PATHMAP];
  647. PathFindMap[SIMPLE_PATHMAP] = NULL;
  648. }
  649. #ifdef DEBUG_GLOBALMAP_BUILD
  650. GameLog::cleanup();
  651. #endif
  652. }
  653. //---------------------------------------------------------------------------
  654. void* MissionMap::operator new (size_t ourSize) {
  655. void* result = systemHeap->Malloc(ourSize);
  656. return(result);
  657. }
  658. //---------------------------------------------------------------------------
  659. void MissionMap::operator delete (void* us) {
  660. systemHeap->Free(us);
  661. }
  662. //---------------------------------------------------------------------------
  663. void MissionMap::init (long h, long w) {
  664. height = h;
  665. width = w;
  666. VerticesMapSideDivTwo = (Terrain::blocksMapSide * Terrain::verticesBlockSide) / 2.0;
  667. MetersMapSideDivTwo = Terrain::worldUnitsMapSide / 2;
  668. MapCellDiagonal = Terrain::worldUnitsPerCell * 1.4142 * metersPerWorldUnit;
  669. HalfMapCell = Terrain::worldUnitsPerCell / 2.0;
  670. for (long i = 0; i < MAX_MAP_CELL_WIDTH; i++)
  671. tileMulMAPCELL_DIM[i] = i * MAPCELL_DIM;
  672. long tileWidth = width / MAPCELL_DIM;
  673. long tileHeight = height / MAPCELL_DIM;
  674. for (i = 0; i < tileHeight; i++)
  675. Terrain::tileRowToWorldCoord[i] = (Terrain::worldUnitsMapSide / 2.0) - (i * Terrain::worldUnitsPerVertex);
  676. for (i = 0; i < tileWidth; i++)
  677. Terrain::tileColToWorldCoord[i] = (i * Terrain::worldUnitsPerVertex) - (Terrain::worldUnitsMapSide / 2.0);
  678. for (i = 0; i < MAPCELL_DIM; i++)
  679. Terrain::cellToWorldCoord[i] = (Terrain::worldUnitsPerVertex / (float)MAPCELL_DIM) * i;
  680. for (i = 0; i < height; i++)
  681. Terrain::cellRowToWorldCoord[i] = (Terrain::worldUnitsMapSide / 2.0) - (i * Terrain::worldUnitsPerCell);
  682. for (i = 0; i < width; i++)
  683. Terrain::cellColToWorldCoord[i] = (i * Terrain::worldUnitsPerCell) - (Terrain::worldUnitsMapSide / 2.0);
  684. if (map) {
  685. systemHeap->Free(map);
  686. map = NULL;
  687. }
  688. long mapSize = sizeof(MapCell) * width * height;
  689. if (mapSize > 0)
  690. map = (MapCellPtr)systemHeap->Malloc(sizeof(MapCell) * width * height);
  691. gosASSERT(map != NULL);
  692. memclear(map, sizeof(MapCell) * width * height);
  693. }
  694. //---------------------------------------------------------------------------
  695. #define NUM_MISSIONMAP_PACKETS 4
  696. long MissionMap::init (PacketFile* packetFile, long whichPacket) {
  697. packetFile->readPacket(whichPacket++, (unsigned char*)&height);
  698. packetFile->readPacket(whichPacket++, (unsigned char*)&width);
  699. packetFile->readPacket(whichPacket++, (unsigned char*)&planet);
  700. init(height, width);
  701. packetFile->readPacket(whichPacket++, (unsigned char*)map);
  702. //-----------------------
  703. // Just some debugging...
  704. //long numOffMap = 0;
  705. //for (long r = 0; r < height; r++)
  706. // for (long c = 0; c < width; c++) {
  707. // Stuff::Vector3D worldPos;
  708. // land->cellToWorld(r, c, worldPos);
  709. // bool set = (land->IsEditorSelectTerrainPosition(worldPos) && !land->IsGameSelectTerrainPosition(worldPos));
  710. // GameMap->setOffMap(r, c, set);
  711. // if (set)
  712. // numOffMap++;
  713. // }
  714. for (long r = 0; r < height; r++)
  715. for (long c = 0; c < width; c++) {
  716. GameMap->setMover(r, c, false);
  717. }
  718. return(NUM_MISSIONMAP_PACKETS);
  719. }
  720. //---------------------------------------------------------------------------
  721. long MissionMap::write (PacketFile* packetFile, long whichPacket) {
  722. //-------------------------------------------
  723. // Return number of packets if !packetFile...
  724. if (!packetFile)
  725. return(NUM_MISSIONMAP_PACKETS);
  726. long result = packetFile->writePacket(whichPacket++, (unsigned char*)&height, sizeof(long));
  727. if (result <= 0)
  728. Fatal(result, " MissionMap.write: Unable to write height packet ");
  729. result = packetFile->writePacket(whichPacket++, (unsigned char*)&width, sizeof(long));
  730. if (result <= 0)
  731. Fatal(result, " MissionMap.write: Unable to write width packet ");
  732. result = packetFile->writePacket(whichPacket++, (unsigned char*)&planet, sizeof(long));
  733. if (result <= 0)
  734. Fatal(result, " MissionMap.write: Unable to write planet packet ");
  735. result = packetFile->writePacket(whichPacket++, (unsigned char*)map, sizeof(MapCell) * height * width);
  736. if (result <= 0)
  737. Fatal(result, " MissionMap.write: Unable to write map packet ");
  738. return(NUM_MISSIONMAP_PACKETS);
  739. }
  740. //---------------------------------------------------------------------------
  741. bool MissionMap::getPassable (Stuff::Vector3D cellPosition) {
  742. long row, col;
  743. land->worldToCell(cellPosition, row, col);
  744. return(getPassable(row, col));
  745. }
  746. //---------------------------------------------------------------------------
  747. long MissionMap::init (long cellHeight, long cellWidth, long curPlanet, MissionMapCellInfo* mapData) {
  748. if (!map)
  749. init(cellHeight, cellWidth);
  750. planet = curPlanet;
  751. //---------------------------------------------------------------------------
  752. // Be damned sure map is empty. This is required or mines will NOT go away.
  753. memset(map, 0, sizeof(MapCell) * width * height);
  754. long terrainTypes[1024];
  755. long overlayTypes[1024];
  756. for (long i = 0; i < 256; i++)
  757. terrainTypes[i] = overlayTypes[i] = 0;
  758. if (mapData)
  759. for (long row = 0; row < height; row++)
  760. for (long col = 0; col < width; col++) {
  761. setTerrain(row, col, mapData[row * width + col].terrain);
  762. terrainTypes[mapData[row * width + col].terrain]++;
  763. setOverlay(row, col, mapData[row * width + col].overlay);
  764. overlayTypes[mapData[row * width + col].overlay]++;
  765. setGate(row, col, mapData[row * width + col].gate);
  766. setRoad(row, col, mapData[row * width + col].road);
  767. setPassable(row, col, mapData[row * width + col].passable);
  768. setLocalHeight(row, col, mapData[row * width + col].lineOfSight);
  769. setPreserved(row, col, false);
  770. setMine(row, col, mapData[row * width + col].mine);
  771. setForest(row, col, mapData[row * width + col].forest);
  772. setBuildWall(row, col, false);
  773. setBuildGate(row, col, false);
  774. Stuff::Vector3D worldPos;
  775. land->cellToWorld(row, col, worldPos);
  776. long cellWaterType = land->getWater(worldPos);
  777. if (cellWaterType == WATER_TYPE_SHALLOW)
  778. setShallowWater(row, col, true);
  779. if (cellWaterType == WATER_TYPE_DEEP)
  780. setDeepWater(row, col, true);
  781. for (long i = 0; i < NUM_MOVE_LEVELS; i++)
  782. GameMap->setPathlock(i, row, col, false);
  783. switch (mapData[row * width + col].specialType) {
  784. case SPECIAL_WALL:
  785. setBuildWall(row, col, true);
  786. break;
  787. case SPECIAL_GATE:
  788. setBuildGate(row, col, true);
  789. break;
  790. case SPECIAL_LAND_BRIDGE:
  791. setBuildLandBridge(row, col, true);
  792. break;
  793. //case SPECIAL_FOREST:
  794. // setBuildForest(row, col, true);
  795. // break;
  796. }
  797. }
  798. else {
  799. for (long row = 0; row < height; row++)
  800. for (long col = 0; col < width; col++) {
  801. setTerrain(row, col, MC_GRASS_TYPE);
  802. setOverlay(row, col, 0);
  803. setGate(row, col, false);
  804. if ((row == 0) || (col == 0) || (row == (height - 1)) || (col == (width - 1)))
  805. setPassable(row, col, false);
  806. else
  807. setPassable(row, col, true);
  808. for (long i = 0; i < NUM_MOVE_LEVELS; i++)
  809. GameMap->setPathlock(i, row, col, false);
  810. setPreserved(row, col, false);
  811. setMine(row, col, 0);
  812. }
  813. }
  814. return(0);
  815. }
  816. //---------------------------------------------------------------------------
  817. void MissionMap::setPassable (long row, long col, char* footPrint, bool passable) {
  818. /* FootPrint Data format:
  819. r1, c1, l1, r2, c2, l2, r3, c3, l3, ...
  820. where r1 and c1 are offsets from row,col and l1 is the runlength of
  821. the passable/impassable block of cells. This function keeps reading in the
  822. (r, c, l) sets of data until (-1, -1, -1) is read in. Obviously, the data
  823. is a block of chars, so all values must be -127 < x < 127. This should
  824. allow foot prints big enuff for all buildings we have.
  825. */
  826. char* data = footPrint;
  827. long r = row + *data++;
  828. long c = col + *data++;
  829. long len = *data++;
  830. while (len != -1) {
  831. MapCellPtr cell = &map[r * width + c];
  832. for (long i = 0; i < len; i++) {
  833. cell->setPassable(passable);
  834. cell++;
  835. }
  836. r = row + *data++;
  837. c = col + *data++;
  838. len = *data++;
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void MissionMap::spreadState (long cellRow, long cellCol, long radius) {
  843. //---------------------------------------------------------------------------
  844. // This does a simple depth-first spread from the center. The way it's coded,
  845. // it essentially grows a box from the center cell.
  846. if (inMapBounds(cellRow, cellCol, height, width) && (radius > 0)) {
  847. //-------------------------------------------------------------------
  848. // If we're placing moving objects for this frame only, we'll want to
  849. // preserve the cell states of this tile...
  850. if (!getPreserved(cellRow, cellCol))
  851. setPreserved(cellRow, cellCol, true);
  852. map[cellRow * width + cellCol].setPassable(false);
  853. for (long dir = 0; dir < NUM_DIRECTIONS; dir++) {
  854. long adjR = cellRow;
  855. long adjC = cellCol;
  856. calcAdjNode(adjR, adjC, dir);
  857. spreadState(adjR, adjC, radius - 1);
  858. }
  859. }
  860. }
  861. //---------------------------------------------------------------------------
  862. long MissionMap::placeObject (Stuff::Vector3D position, float radius) {
  863. long cellRow, cellCol;
  864. land->worldToCell(position, cellRow, cellCol);
  865. //---------------------------------------------------------
  866. // Now, set the cell states based upon the object's size...
  867. float cellRadius = (radius / (Terrain::worldUnitsPerCell * metersPerWorldUnit));
  868. if ((cellRadius > 0.5) && (cellRadius < 1.0))
  869. cellRadius = 1.0;
  870. spreadState(cellRow, cellCol, (long)cellRadius);
  871. return(NO_ERR);
  872. }
  873. //---------------------------------------------------------------------------
  874. void MissionMap::placeTerrainObject(long objectClass,
  875. long objectTypeID,
  876. long cellRow,
  877. long cellCol,
  878. __int64 footPrint,
  879. bool blocksLineOfFire,
  880. long mineType) {
  881. #if 0
  882. Assert(objectClass != -1, 0, " MissionMap.placeTerrainObject: bad ObjectClass ");
  883. long maskTemplateLo = footPrint;
  884. long maskTemplateHi = footPrint >> 32;
  885. long posTileR = cellRow / MAPCELL_DIM;
  886. long posTileC = cellCol / MAPCELL_DIM;
  887. long tileOriginR = posTileR;
  888. long tileOriginC = posTileC;
  889. long startCellR = cellRow % MAPCELL_DIM;
  890. long startCellC = cellCol % MAPCELL_DIM;
  891. long cellR = startCellR - 4;
  892. long cellC = startCellC - 4;
  893. while (cellR < 0) {
  894. posTileR--;
  895. cellR += MAPCELL_DIM;
  896. }
  897. while (cellC < 0) {
  898. posTileC--;
  899. cellC += MAPCELL_DIM;
  900. }
  901. if (objectClass == GATE)
  902. blocksLineOfFire = false;
  903. long startCol = posTileC;
  904. long firstCellC = cellC;
  905. //------------------------------------------------------------------
  906. // We now use the objectClass to do this correctly.
  907. if ((objectClass == BUILDING) ||
  908. (objectClass == TREEBUILDING) ||
  909. (objectClass == GATE) ||
  910. (objectClass == TURRET) ||
  911. (objectClass == TERRAINOBJECT)) {
  912. if (maskTemplateLo || maskTemplateHi) {
  913. //---------------------------------------------------------------------
  914. // We also need to set the BUILDING flag in the scenario map to indicate
  915. // a building exists in this tile...
  916. //map[tileOriginR * width + tileOriginC].setBuildingHere(true);
  917. switch (objectTypeID) {
  918. case 644:
  919. case 647:
  920. case 690:
  921. case 691:
  922. map[tileOriginR * width + tileOriginC].setOverlayType(OVERLAY_GATE_CLAN_EW_CLOSED);
  923. break;
  924. case 645:
  925. case 646:
  926. case 693:
  927. case 692:
  928. map[tileOriginR * width + tileOriginC].setOverlayType(OVERLAY_GATE_CLAN_NS_CLOSED);
  929. break;
  930. }
  931. long bits = 0;
  932. for (long totalCelRSet = 0;totalCelRSet<8;totalCelRSet++) {
  933. for (long totalCelCSet = 0;totalCelCSet<8;totalCelCSet++) {
  934. long bitMask = 1 << bits;
  935. long maskTemplate = maskTemplateLo;
  936. if (bits >= 32) {
  937. bitMask = 1 << (bits-32);
  938. maskTemplate = maskTemplateHi;
  939. }
  940. if ((bitMask & maskTemplate) == bitMask) {
  941. map[posTileR * width + posTileC].setCellPassable(cellR, cellC, 0);
  942. if (blocksLineOfFire)
  943. map[posTileR * width + posTileC].setCellLOS(cellR, cellC, 0);
  944. }
  945. cellC++;
  946. if (cellC == MAPCELL_DIM) {
  947. cellC = 0;
  948. posTileC++;
  949. }
  950. bits++;
  951. }
  952. posTileC = startCol;
  953. cellC = firstCellC;
  954. cellR++;
  955. if (cellR == MAPCELL_DIM) {
  956. cellR = 0;
  957. posTileR++;
  958. }
  959. }
  960. }
  961. }
  962. else if (objectClass == MINE) {
  963. //-----------------------------------------------------------
  964. // We need to know what cell is in the center of this MINE
  965. // We then mark the tile as MINED!!!!
  966. // We base it on the MineTypeNumber stored in the Mine Class.
  967. if (mineType == 3) //Force into only one slots instead of three
  968. mineType = 2;
  969. if (mineType == 1) //Force into only one slots instead of three
  970. mineType = 2;
  971. if (mineType == 13) //Force into only one slots instead of three
  972. mineType = 12;
  973. if (mineType == 11) //Force into only one slots instead of three
  974. mineType = 12;
  975. if (mineType > 10) // 10-07-98 HKG hack! Mines are off by a tile in each direction
  976. map[(tileOriginR) * width + tileOriginC].setInnerSphereMine(mineType - 10);
  977. else
  978. map[(tileOriginR)* width + tileOriginC].setClanMine(mineType);
  979. }
  980. #endif
  981. }
  982. //---------------------------------------------------------------------------
  983. long MissionMap::getOverlayWeight (long cellR, long cellC, long moverOverlayWeightClass, long moverRelation) {
  984. //long overlay = getOverlay(cellR, cellC);
  985. /*
  986. if (overlay) {
  987. long* overlayWeightTable = &OverlayWeightTable[moverOverlayWeightClass * NUM_OVERLAY_TYPES * MAPCELL_DIM * MAPCELL_DIM];
  988. if ((overlayType >= OVERLAY_GATE_CLAN_NS_OPEN) && (overlayType <= OVERLAY_GATE_IS_EW_CLOSED)) {
  989. overlayType = GateOverlayTable[moverRelation][overlayType - OVERLAY_GATE_CLAN_NS_OPEN];
  990. if (overlayType == -1)
  991. return(COST_BLOCKED * 2);
  992. else {
  993. long overlayCostIndex = OverlayWeightIndex[overlayType] + cellR * MAPCELL_DIM + cellC;
  994. return(overlayWeightTable[overlayCostIndex]);
  995. }
  996. }
  997. else {
  998. long overlayCostIndex = OverlayWeightIndex[overlayType] + cellR * MAPCELL_DIM + cellC;
  999. return(overlayWeightTable[overlayCostIndex]);
  1000. }
  1001. }
  1002. */
  1003. return(0);
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void MissionMap::print (char* fileName) {
  1007. File* debugFile = new File;
  1008. debugFile->create(fileName);
  1009. for (long cellR = 0; cellR < height; cellR++) {
  1010. char outString[1024];
  1011. outString[0] = NULL;
  1012. for (long cellC = 0; cellC < width; cellC++) {
  1013. if (!map[cellR * width + cellC].getPassable())
  1014. strcat(outString, "X");
  1015. else
  1016. strcat(outString, ".");
  1017. }
  1018. strcat(outString, "\n");
  1019. debugFile->writeString(outString);
  1020. }
  1021. debugFile->writeString("\n");
  1022. debugFile->close();
  1023. delete debugFile;
  1024. debugFile = NULL;
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void MissionMap::destroy (void) {
  1028. if (map) {
  1029. systemHeap->Free(map);
  1030. map = NULL;
  1031. }
  1032. }
  1033. //***************************************************************************
  1034. // MOVE PATH class
  1035. //***************************************************************************
  1036. void* MovePath::operator new (size_t ourSize) {
  1037. void* result;
  1038. result = systemHeap->Malloc(ourSize);
  1039. return(result);
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void MovePath::operator delete (void* us) {
  1043. systemHeap->Free(us);
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. long MovePath::init (long numberOfSteps) {
  1047. numSteps = numStepsWhenNotPaused = numberOfSteps;
  1048. static long maxNumberOfSteps = 0;
  1049. if (numberOfSteps > maxNumberOfSteps) {
  1050. maxNumberOfSteps = numberOfSteps;
  1051. return(maxNumberOfSteps);
  1052. }
  1053. for (long i = 0; i < MAX_STEPS_PER_MOVEPATH; i++) {
  1054. stepList[i].distanceToGoal = 0.0;
  1055. stepList[i].destination.x = 0.0;
  1056. stepList[i].destination.y = 0.0;
  1057. stepList[i].destination.z = 0.0;
  1058. stepList[i].direction = 0;
  1059. }
  1060. return(-1);
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. void MovePath::clear (void) {
  1064. init();
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. void MovePath::destroy (void) {
  1068. numSteps = 0;
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. float MovePath::getDistanceLeft (Stuff::Vector3D position, long stepNumber) {
  1072. if (stepNumber == -1)
  1073. stepNumber = curStep;
  1074. float distance = distance_from(position,stepList[stepNumber].destination);
  1075. distance *= metersPerWorldUnit;
  1076. distance += stepList[stepNumber].distanceToGoal;
  1077. return(distance);
  1078. }
  1079. //---------------------------------------------------------------------------
  1080. void MovePath::lock (long level, long start, long range, bool setting) {
  1081. #ifdef _DEBUG
  1082. if (level > 1)
  1083. STOP(("MovePath.lock: bad level %d", level));
  1084. #endif
  1085. if (start == -1)
  1086. start = curStep;
  1087. long lastStep = start + range;
  1088. if (lastStep >= numStepsWhenNotPaused)
  1089. lastStep = numStepsWhenNotPaused;
  1090. for (long i = start; i < lastStep; i++)
  1091. GameMap->setPathlock(level, stepList[i].cell[0], stepList[i].cell[1], setting);
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. bool MovePath::isLocked (long level, long start, long range, bool* reachedEnd) {
  1095. #ifdef _DEBUG
  1096. if (level > 1)
  1097. STOP(("MovePath.isLocked: bad moveLevel %d", level));
  1098. #endif
  1099. if (start == -1)
  1100. start = curStep;
  1101. long lastStep = start + range;
  1102. if (reachedEnd)
  1103. *reachedEnd = false;
  1104. if (lastStep >= numStepsWhenNotPaused) {
  1105. if (reachedEnd)
  1106. *reachedEnd = true;
  1107. lastStep = numStepsWhenNotPaused;
  1108. }
  1109. for (long i = start; i < lastStep; i++)
  1110. if (GameMap->getPathlock(level, stepList[i].cell[0], stepList[i].cell[1]))
  1111. return(true);
  1112. return(false);
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. bool MovePath::isBlocked (long start, long range, bool* reachedEnd) {
  1116. if (start == -1)
  1117. start = curStep;
  1118. long lastStep = start + range;
  1119. if (reachedEnd)
  1120. *reachedEnd = false;
  1121. if (lastStep >= numStepsWhenNotPaused) {
  1122. if (reachedEnd)
  1123. *reachedEnd = true;
  1124. lastStep = numStepsWhenNotPaused;
  1125. }
  1126. for (long i = start; i < lastStep; i++)
  1127. if (!GameMap->getPassable(stepList[i].cell[0], stepList[i].cell[1]))
  1128. return(true);
  1129. return(false);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. long MovePath::crossesBridge (long start, long range) {
  1133. if (start == -1)
  1134. start = curStep;
  1135. long lastStep = start + range;
  1136. if (lastStep >= numStepsWhenNotPaused)
  1137. lastStep = numStepsWhenNotPaused;
  1138. //We need to redo this code when bridges are in cause they are objects now!!
  1139. #if 0
  1140. for (long i = start; i < lastStep; i++) {
  1141. long overlayType = GameMap->getOverlay(stepList[i].cell[0], stepList[i].cell[1]);
  1142. if (OverlayIsBridge[overlayType])
  1143. return(GlobalMoveMap[0]->calcArea(stepList[i].cell[0], stepList[i].cell[1]));
  1144. }
  1145. #endif
  1146. return(-1);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. long MovePath::crossesCell (long start, long range, long cellR, long cellC) {
  1150. if (start == -1)
  1151. start = curStep;
  1152. long lastStep = start + range;
  1153. if (lastStep >= numStepsWhenNotPaused)
  1154. lastStep = numStepsWhenNotPaused;
  1155. for (long i = start; i < lastStep; i++) {
  1156. if ((cellR == stepList[i].cell[0]) && (cellC == stepList[i].cell[1]))
  1157. return(i);
  1158. }
  1159. return(-1);
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. long MovePath::crossesClosedClanGate (long start, long range) {
  1163. return(-1);
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. long MovePath::crossesClosedISGate (long start, long range) {
  1167. return(-1);
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. long MovePath::crossesClosedGate (long start, long range) {
  1171. if (start == -1)
  1172. start = curStep;
  1173. long lastStep = start + range;
  1174. if (lastStep >= numStepsWhenNotPaused)
  1175. lastStep = numStepsWhenNotPaused;
  1176. //Probably should have redone this code cause gates aren't overlays anymore?
  1177. #if 0
  1178. for (long i = start; i < lastStep; i++) {
  1179. long overlayType = GameMap->getOverlay(stepList[i].cell[0], stepList[i].cell[1]);
  1180. if (OverlayIsClosedGate[overlayType])
  1181. return(i);
  1182. }
  1183. #endif
  1184. return(-1);
  1185. }
  1186. //**********************************************************************************
  1187. // GLOBAL MAP class
  1188. //**********************************************************************************
  1189. void* GlobalMap::operator new (size_t ourSize) {
  1190. void* result = systemHeap->Malloc(ourSize);
  1191. return(result);
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void GlobalMap::operator delete (void* us) {
  1195. systemHeap->Free(us);
  1196. }
  1197. //------------------------------------------------------------------------------------------
  1198. void GlobalMap::init (long w, long h) {
  1199. width = w;
  1200. height = h;
  1201. areaMap = (short*)systemHeap->Malloc(sizeof(short) * w * h);
  1202. gosASSERT(areaMap != NULL);
  1203. for (long r = 0; r < height; r++)
  1204. for (long c = 0; c < width; c++)
  1205. areaMap[r * width + c] = -1;
  1206. gosASSERT(((width % SECTOR_DIM) == 0) && ((height % SECTOR_DIM) == 0));
  1207. sectorWidth = w / SECTOR_DIM;
  1208. sectorHeight = w / SECTOR_DIM;
  1209. numAreas = 0;
  1210. areas = NULL;
  1211. numDoors = 0;
  1212. doors = NULL;
  1213. doorBuildList = NULL;
  1214. pathExistsTable = NULL;
  1215. #ifdef USE_PATH_COST_TABLE
  1216. pathCostTable = NULL;
  1217. #endif
  1218. blank = false;
  1219. }
  1220. //------------------------------------------------------------------------------------------
  1221. #define GLOBALMAP_VERSION_NUMBER 0x022523 // My 51st birthday:)
  1222. long GlobalMap::init (PacketFilePtr packetFile, long whichPacket) {
  1223. long startPacket = whichPacket;
  1224. unsigned long version = 0;
  1225. long result = packetFile->readPacket(whichPacket++, (unsigned char*)&version);
  1226. if (result == 0)
  1227. Fatal(result, " GlobalMap.init: unable to read version packet ");
  1228. bool badVersion = false;
  1229. if (version != GLOBALMAP_VERSION_NUMBER)
  1230. badVersion = true;
  1231. result = packetFile->readPacket(whichPacket++, (unsigned char*)&height);
  1232. if (result == 0)
  1233. Fatal(result, " GlobalMap.init: unable to read height packet ");
  1234. result = packetFile->readPacket(whichPacket++, (unsigned char*)&width);
  1235. if (result == 0)
  1236. Fatal(result, " GlobalMap.init: unable to read width packet ");
  1237. long sectorDim; // this is no longer used...
  1238. result = packetFile->readPacket(whichPacket++, (unsigned char*)&sectorDim);
  1239. if (result == 0)
  1240. Fatal(result, " GlobalMap.init: unable to read sectorDim packet ");
  1241. result = packetFile->readPacket(whichPacket++, (unsigned char*)&sectorHeight);
  1242. if (result == 0)
  1243. Fatal(result, " GlobalMap.init: unable to read sectorHeight packet ");
  1244. result = packetFile->readPacket(whichPacket++, (unsigned char*)&sectorWidth);
  1245. if (result == 0)
  1246. Fatal(result, " GlobalMap.init: unable to read sectorWidth packet ");
  1247. result = packetFile->readPacket(whichPacket++, (unsigned char*)&numAreas);
  1248. if (result == 0)
  1249. Fatal(result, " GlobalMap.init: unable to read numAreas packet ");
  1250. result = packetFile->readPacket(whichPacket++, (unsigned char*)&numDoors);
  1251. if (result == 0)
  1252. Fatal(result, " GlobalMap.init: unable to read numDoors packet ");
  1253. result = packetFile->readPacket(whichPacket++, (unsigned char*)&numDoorInfos);
  1254. if (result == 0)
  1255. Fatal(result, " GlobalMap.init: unable to read numDoorInfos packet ");
  1256. result = packetFile->readPacket(whichPacket++, (unsigned char*)&numDoorLinks);
  1257. if (result == 0)
  1258. Fatal(result, " GlobalMap.init: unable to read numDoorLinks packet ");
  1259. if (badVersion) {
  1260. //---------------------------------------------------------
  1261. // Bad version map, so return number of packets but bail...
  1262. badLoad = true;
  1263. #ifdef USE_PATH_COST_TABLE
  1264. pathCostTable = NULL;
  1265. return(14 + numDoorInfos + (numDoors + NUM_DOOR_OFFSETS) * 2);
  1266. #else
  1267. return(13 + numDoorInfos + (numDoors + NUM_DOOR_OFFSETS) * 2);
  1268. #endif
  1269. }
  1270. areaMap = (short*)systemHeap->Malloc(sizeof(short) * height * width);
  1271. gosASSERT(areaMap != NULL);
  1272. result = packetFile->readPacket(whichPacket++, (unsigned char*)areaMap);
  1273. if (result == 0)
  1274. Fatal(result, " GlobalMap.init: unable to read areaMap packet ");
  1275. areas = (GlobalMapAreaPtr)systemHeap->Malloc(sizeof(GlobalMapArea) * numAreas);
  1276. gosASSERT(areas != NULL);
  1277. result = packetFile->readPacket(whichPacket++, (unsigned char*)areas);
  1278. if (result == 0)
  1279. Fatal(result, " GlobalMap.init: unable to read areas packet ");
  1280. doorInfos = (DoorInfoPtr)systemHeap->Malloc(sizeof(DoorInfo) * numDoorInfos);
  1281. gosASSERT(doorInfos != NULL);
  1282. long curDoorInfo = 0;
  1283. for (long i = 0; i < numAreas; i++)
  1284. if (areas[i].numDoors) {
  1285. result = packetFile->readPacket(whichPacket++, (unsigned char*)&doorInfos[curDoorInfo]);
  1286. if (result == 0)
  1287. Fatal(result, " GlobalMap.init: unable to read doorInfos packet ");
  1288. curDoorInfo += areas[i].numDoors;
  1289. }
  1290. Assert(numDoorInfos == curDoorInfo, 0, " GlobalMap.init: bad doorInfo count ");
  1291. //------------------------------------------------------------
  1292. // Set up the areas so they point to the correct door infos...
  1293. curDoorInfo = 0;
  1294. for (long curArea = 0; curArea < numAreas; curArea++) {
  1295. areas[curArea].doors = &doorInfos[curDoorInfo];
  1296. curDoorInfo += areas[curArea].numDoors;
  1297. }
  1298. doors = (GlobalMapDoorPtr)systemHeap->Malloc(sizeof(GlobalMapDoor) * (numDoors + NUM_DOOR_OFFSETS));
  1299. gosASSERT(doors != NULL);
  1300. result = packetFile->readPacket(whichPacket++, (unsigned char*)doors);
  1301. if (result == 0)
  1302. Fatal(result, " GlobalMap.init: unable to read doors packet ");
  1303. //--------------
  1304. // Door Links...
  1305. doorLinks = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * numDoorLinks);
  1306. gosASSERT(doorLinks != NULL);
  1307. long numLinksRead = 0;
  1308. for (i = 0; i < (numDoors + NUM_DOOR_OFFSETS); i++) {
  1309. long numLinks = doors[i].numLinks[0] + NUM_EXTRA_DOOR_LINKS;
  1310. gosASSERT(numLinks >= 2);
  1311. result = packetFile->readPacket(whichPacket++, (unsigned char*)&doorLinks[numLinksRead]);
  1312. if (result <= 0)
  1313. Fatal(result, " GlobalMap.init: Unable to write doorLinks packet ");
  1314. doors[i].links[0] = &doorLinks[numLinksRead];
  1315. numLinksRead += numLinks;
  1316. numLinks = doors[i].numLinks[1] + NUM_EXTRA_DOOR_LINKS;
  1317. gosASSERT(numLinks >= 2);
  1318. result = packetFile->readPacket(whichPacket++, (unsigned char*)&doorLinks[numLinksRead]);
  1319. if (result <= 0)
  1320. Fatal(result, " GlobalMap.init: Unable to write doorLinks packet ");
  1321. doors[i].links[1] = &doorLinks[numLinksRead];
  1322. numLinksRead += numLinks;
  1323. }
  1324. Assert(numLinksRead == numDoorLinks, 0, " GlobalMap.init: Incorrect Links count ");
  1325. numOffMapAreas = 0;
  1326. for (i = 0; i < numAreas; i++)
  1327. if (areas[i].offMap) {
  1328. offMapAreas[numOffMapAreas++] = i;
  1329. closeArea(i);
  1330. }
  1331. else
  1332. openArea(i);
  1333. //--------------------------
  1334. // Create pathExistsTable...
  1335. pathExistsTable = (unsigned char*)systemHeap->Malloc(numAreas * (numAreas / 4 + 1));
  1336. if (!pathExistsTable)
  1337. STOP(("GlobalMap.init: unable to malloc pathExistsTable"));
  1338. clearPathExistsTable();
  1339. if (logEnabled && !blank) {
  1340. char s[256];
  1341. for (long i = 0; i < numDoors; i++) {
  1342. sprintf(s, "door %05d, %s(%d), areas %d & %d", i, doors[i].open ? "opened" : "CLOSED", doors[i].teamID, doors[i].area[0], doors[i].area[1]);
  1343. log->write(s);
  1344. for (long side = 0; side < 2; side++) {
  1345. sprintf(s, " side %d", side);
  1346. log->write(s);
  1347. for (long j = 0; j < doors[i].numLinks[side]; j++) {
  1348. sprintf(s, " link %03d, to door %05d, cost %d",
  1349. j,
  1350. doors[i].links[side][j].doorIndex,
  1351. doors[i].links[side][j].cost);
  1352. log->write(s);
  1353. }
  1354. }
  1355. }
  1356. log->write(" ");
  1357. for (i = 0; i < numAreas; i++) {
  1358. sprintf(s, "area %05d, %s(%d)", i, areas[i].open ? "opened" : "CLOSED", areas[i].teamID);
  1359. log->write(s);
  1360. if (areas[i].ownerWID > 0) {
  1361. sprintf(s, " ownerWID is %d", areas[i].ownerWID);
  1362. log->write(s);
  1363. }
  1364. static char* typeString[] = {
  1365. "normal area",
  1366. "wall area",
  1367. "gate area",
  1368. "land bridge area",
  1369. "forest area"
  1370. };
  1371. sprintf(s, " %s", typeString[areas[i].type]);
  1372. log->write(s);
  1373. for (long d = 0; d < areas[i].numDoors; d++) {
  1374. sprintf(s, " door %03d is %d (%d:%d & %d) ",
  1375. d, areas[i].doors[d].doorIndex, areas[i].doors[d].doorSide,
  1376. doors[areas[i].doors[d].doorIndex].area[0], doors[areas[i].doors[d].doorIndex].area[1]);
  1377. log->write(s);
  1378. }
  1379. }
  1380. log->write(" ");
  1381. }
  1382. return(whichPacket - startPacket);
  1383. }
  1384. //------------------------------------------------------------------------------------------
  1385. long GlobalMap::write (PacketFilePtr packetFile, long whichPacket) {
  1386. //---------------------------------------------------------
  1387. // If mapFile NULL, return the number of packets we need...
  1388. long numDrInfos = 0;
  1389. for (long i = 0; i < numAreas; i++)
  1390. if (areas[i].numDoors)
  1391. numDrInfos++;
  1392. #ifdef USE_PATH_COST_TABLE
  1393. long numPackets = 14 + numDrInfos + (numDoors + NUM_DOOR_OFFSETS) * 2;
  1394. #else
  1395. long numPackets = 13 + numDrInfos + (numDoors + NUM_DOOR_OFFSETS) * 2;
  1396. #endif
  1397. if (!packetFile)
  1398. return(numPackets);
  1399. unsigned long version = GLOBALMAP_VERSION_NUMBER;
  1400. long result = packetFile->writePacket(whichPacket++, (unsigned char*)&version, sizeof(long));
  1401. if (result <= 0)
  1402. Fatal(result, " GlobalMap.write: Unable to write version packet ");
  1403. result = packetFile->writePacket(whichPacket++, (unsigned char*)&height, sizeof(long));
  1404. if (result <= 0)
  1405. Fatal(result, " GlobalMap.write: Unable to write height packet ");
  1406. result = packetFile->writePacket(whichPacket++, (unsigned char*)&width, sizeof(long));
  1407. if (result <= 0)
  1408. Fatal(result, " GlobalMap.write: Unable to write width packet ");
  1409. long sectorDim = SECTOR_DIM;
  1410. result = packetFile->writePacket(whichPacket++, (unsigned char*)&sectorDim, sizeof(long));
  1411. if (result <= 0)
  1412. Fatal(result, " GlobalMap.write: Unable to write sectorDim packet ");
  1413. result = packetFile->writePacket(whichPacket++, (unsigned char*)&sectorHeight, sizeof(long));
  1414. if (result <= 0)
  1415. Fatal(result, " GlobalMap.write: Unable to write sectorHeight packet ");
  1416. result = packetFile->writePacket(whichPacket++, (unsigned char*)&sectorWidth, sizeof(long));
  1417. if (result <= 0)
  1418. Fatal(result, " GlobalMap.write: Unable to write sectorWidth packet ");
  1419. result = packetFile->writePacket(whichPacket++, (unsigned char*)&numAreas, sizeof(long));
  1420. if (result <= 0)
  1421. Fatal(result, " GlobalMap.write: Unable to write numAreas packet ");
  1422. result = packetFile->writePacket(whichPacket++, (unsigned char*)&numDoors, sizeof(long));
  1423. if (result <= 0)
  1424. Fatal(result, " GlobalMap.write: Unable to write numDoors packet ");
  1425. result = packetFile->writePacket(whichPacket++, (unsigned char*)&numDoorInfos, sizeof(long));
  1426. if (result <= 0)
  1427. Fatal(result, " GlobalMap.write: Unable to write numDoorInfos packet ");
  1428. result = packetFile->writePacket(whichPacket++, (unsigned char*)&numDoorLinks, sizeof(long));
  1429. if (result <= 0)
  1430. Fatal(result, " GlobalMap.write: Unable to write numDoorLinks packet ");
  1431. result = packetFile->writePacket(whichPacket++, (unsigned char*)areaMap, sizeof(short) * height * width);
  1432. if (result <= 0)
  1433. Fatal(result, " GlobalMap.write: Unable to write areaMap packet ");
  1434. //----------------------------------
  1435. // Write out the areas themselves...
  1436. DoorInfoPtr doorInfoSave[MAX_GLOBALMAP_AREAS];
  1437. for (i = 0; i < numAreas; i++) {
  1438. //-------------------------
  1439. // clear it for the save...
  1440. doorInfoSave[i] = areas[i].doors;
  1441. areas[i].doors = NULL;
  1442. areas[i].ownerWID = 0;
  1443. areas[i].teamID = -1;
  1444. //areas[i].offMap = false;
  1445. areas[i].open = true;
  1446. }
  1447. result = packetFile->writePacket(whichPacket++, (unsigned char*)areas, sizeof(GlobalMapArea) * numAreas);
  1448. for (i = 0; i < numAreas; i++) {
  1449. //---------------------
  1450. // restore the doors...
  1451. areas[i].doors = doorInfoSave[i];
  1452. }
  1453. if (result <= 0)
  1454. Fatal(result, " GlobalMap.write: Unable to write areas packet ");
  1455. //------------------------------------
  1456. // Now, write out all the DoorInfos...
  1457. long maxDoors = 0;
  1458. for (long d = 0; d < numDoors; d++) {
  1459. for (long s = 0; s < 2; s++) {
  1460. long areaNumDoors = areas[doors[d].area[s]].numDoors;
  1461. if (areaNumDoors > maxDoors)
  1462. maxDoors = areaNumDoors;
  1463. }
  1464. }
  1465. doors[numDoors + DOOR_OFFSET_START].numLinks[0] = maxDoors;
  1466. doors[numDoors + DOOR_OFFSET_START].numLinks[1] = 0;
  1467. doors[numDoors + DOOR_OFFSET_GOAL].numLinks[0] = maxDoors;
  1468. doors[numDoors + DOOR_OFFSET_GOAL].numLinks[1] = 0;
  1469. long numDoorInfosWritten = 0;
  1470. for (i = 0; i < numAreas; i++)
  1471. if (areas[i].numDoors) {
  1472. long packetSize = sizeof(DoorInfo) * areas[i].numDoors;
  1473. result = packetFile->writePacket(whichPacket++, (unsigned char*)areas[i].doors, packetSize);
  1474. if (result <= 0)
  1475. Fatal(result, " GlobalMap.write: Unable to write doorInfos packet ");
  1476. numDoorInfosWritten += areas[i].numDoors;
  1477. }
  1478. Assert(numDoorInfosWritten == numDoorInfos, 0, " GlobalMap.write: Error in writing DoorInfos ");
  1479. DoorLinkPtr doorLinkSave[MAX_GLOBALMAP_DOORS][2];
  1480. for (i = 0; i < numDoors + NUM_DOOR_OFFSETS; i++) {
  1481. //-------------------------
  1482. // clear it for the save...
  1483. doorLinkSave[i][0] = doors[i].links[0];
  1484. doorLinkSave[i][1] = doors[i].links[1];
  1485. //doors[i].cost = 0;
  1486. doors[i].links[0] = NULL;
  1487. doors[i].links[1] = NULL;
  1488. doors[i].parent = 0;
  1489. doors[i].fromAreaIndex = 0;
  1490. doors[i].flags = 0;
  1491. doors[i].g = 0;
  1492. doors[i].hPrime = 0;
  1493. doors[i].fPrime = 0;
  1494. }
  1495. result = packetFile->writePacket(whichPacket++, (unsigned char*)doors, sizeof(GlobalMapDoor) * (numDoors + NUM_DOOR_OFFSETS));
  1496. for (i = 0; i < (numDoors + NUM_DOOR_OFFSETS); i++) {
  1497. //---------------------
  1498. // restore the links...
  1499. doors[i].links[0] = doorLinkSave[i][0];
  1500. doors[i].links[1] = doorLinkSave[i][1];
  1501. }
  1502. if (result <= 0)
  1503. Fatal(result, " GlobalMap.write: Unable to write doors packet ");
  1504. //------------------------------------
  1505. // Now, write out all the DoorLinks...
  1506. long numberL = 0;
  1507. for (i = 0; i < (numDoors + NUM_DOOR_OFFSETS); i++) {
  1508. long numLinks = doors[i].numLinks[0] + NUM_EXTRA_DOOR_LINKS;
  1509. gosASSERT(numLinks >= 2);
  1510. result = packetFile->writePacket(whichPacket++, (unsigned char*)doors[i].links[0], sizeof(DoorLink) * numLinks);
  1511. if (result <= 0)
  1512. Fatal(result, " GlobalMap.write: Unable to write doorLinks packet ");
  1513. numberL += numLinks;
  1514. numLinks = doors[i].numLinks[1] + NUM_EXTRA_DOOR_LINKS;
  1515. gosASSERT(numLinks >= 2);
  1516. result = packetFile->writePacket(whichPacket++, (unsigned char*)doors[i].links[1], sizeof(DoorLink) * numLinks);
  1517. if (result <= 0)
  1518. Fatal(result, " GlobalMap.write: Unable to write doorLinks packet ");
  1519. numberL += numLinks;
  1520. }
  1521. if (numberL != numDoorLinks)
  1522. PAUSE(("Number of DoorLinks Calculated does not match numDoorLinks"));
  1523. #ifdef USE_PATH_COST_TABLE
  1524. if (blank)
  1525. calcPathCostTable();
  1526. else
  1527. initPathCostTable();
  1528. result = packetFile->writePacket(whichPacket++, (unsigned char*)pathCostTable, numAreas * numAreas);
  1529. if (result <= 0)
  1530. Fatal(result, " GlobalMap.write: Unable to write doors packet ");
  1531. #endif
  1532. return(numPackets);
  1533. }
  1534. //------------------------------------------------------------------------------------------
  1535. void GlobalMap::calcSpecialAreas (MissionMapCellInfo* mapData) {
  1536. bool areaLogged[64000];
  1537. memset(areaLogged, false, 64000);
  1538. long biggestID = -1;
  1539. long smallestID = 99999;
  1540. for (long r = 0; r < height; r++)
  1541. for (long c = 0; c < width; c++) {
  1542. if (mapData[r * width + c].specialType != SPECIAL_NONE) {
  1543. long ID = mapData[r * width + c].specialID;
  1544. if (ID > biggestID)
  1545. biggestID = ID;
  1546. if (ID < smallestID)
  1547. smallestID = ID;
  1548. switch (mapData[r * width + c].specialType) {
  1549. case SPECIAL_GATE:
  1550. if (!areaLogged[ID]) {
  1551. specialAreas[ID].type = SPECIAL_GATE;
  1552. specialAreas[ID].numSubAreas = 0;
  1553. specialAreas[ID].numCells = 0;
  1554. areaLogged[ID] = true;
  1555. }
  1556. areaMap[r * width + c] = ID;
  1557. GameMap->setBuildGate(r, c, true);
  1558. specialAreas[ID].cells[specialAreas[ID].numCells][0] = r;
  1559. specialAreas[ID].cells[specialAreas[ID].numCells][1] = c;
  1560. specialAreas[ID].numCells++;
  1561. break;
  1562. case SPECIAL_WALL:
  1563. if (!areaLogged[ID]) {
  1564. specialAreas[ID].type = SPECIAL_WALL;
  1565. specialAreas[ID].numSubAreas = 0;
  1566. specialAreas[ID].numCells = 0;
  1567. areaLogged[ID] = true;
  1568. }
  1569. areaMap[r * width + c] = ID;
  1570. GameMap->setBuildWall(r, c, true);
  1571. specialAreas[ID].cells[specialAreas[ID].numCells][0] = r;
  1572. specialAreas[ID].cells[specialAreas[ID].numCells][1] = c;
  1573. specialAreas[ID].numCells++;
  1574. break;
  1575. case SPECIAL_LAND_BRIDGE:
  1576. if (!areaLogged[ID]) {
  1577. specialAreas[ID].type = SPECIAL_LAND_BRIDGE;
  1578. specialAreas[ID].numSubAreas = 0;
  1579. specialAreas[ID].numCells = 0;
  1580. areaLogged[ID] = true;
  1581. }
  1582. areaMap[r * width + c] = ID;
  1583. GameMap->setBuildLandBridge(r, c, true);
  1584. specialAreas[ID].cells[specialAreas[ID].numCells][0] = r;
  1585. specialAreas[ID].cells[specialAreas[ID].numCells][1] = c;
  1586. specialAreas[ID].numCells++;
  1587. break;
  1588. //case SPECIAL_FOREST:
  1589. // if (!areaLogged[ID]) {
  1590. // specialAreas[ID].type = SPECIAL_FOREST;
  1591. // specialAreas[ID].numSubAreas = 0;
  1592. // specialAreas[ID].numCells = 0;
  1593. // areaLogged[ID] = true;
  1594. // }
  1595. // areaMap[r * width + c] = ID;
  1596. // GameMap->setBuildForest(r, c, true);
  1597. // break;
  1598. }
  1599. }
  1600. }
  1601. if (biggestID > -1)
  1602. numSpecialAreas = (biggestID + 1);
  1603. for (long i = 0; i < numSpecialAreas; i++) {
  1604. specialAreas[i].numSubAreas = 0;
  1605. //if (!areaLogged[i])
  1606. // STOP(("GlobalMap.calcSpecialAreas: unlogged special area %d", i));
  1607. }
  1608. }
  1609. //------------------------------------------------------------------------------------------
  1610. long GlobalMap::setTempArea (long tileR, long tileC, long cost) {
  1611. long numStartDoors = 0;
  1612. for (long dir = 0; dir < 4; dir++) {
  1613. long adjR = tileR + adjTile[dir][0];
  1614. long adjC = tileC + adjTile[dir][1];
  1615. long adjArea = calcArea(adjR, adjC);
  1616. if (adjArea > -1) {
  1617. // SET TEMP DOORS HERE...
  1618. // areas[numAreas].doors[numStartDoors].r = tileR;
  1619. // areas[numAreas].doors[numStartDoors].c = tileC;
  1620. // areas[numAreas].doors[numStartDoors].length = 1;
  1621. // areas[numAreas].doors[numStartDoors].direction = dir;
  1622. // areas[numAreas].doors[numStartDoors].area = adjArea;
  1623. // numStartDoors++;
  1624. }
  1625. }
  1626. areas[numAreas].numDoors = numStartDoors;
  1627. //----------------------------
  1628. // Now, setup the temp area...
  1629. areas[numAreas].sectorR = tileR / SECTOR_DIM;
  1630. areas[numAreas].sectorC = tileC / SECTOR_DIM;
  1631. //areas[numAreas].cost = 1;
  1632. return(numAreas);
  1633. }
  1634. //------------------------------------------------------------------------------------------
  1635. bool GlobalMap::fillSpecialArea (long row, long col, long area, long specialID) {
  1636. //----------------------------------------------------------------------
  1637. // This is used to fill any dynamic or "special" areas. Currently, these
  1638. // are: wall, gate and forest.
  1639. areaMap[row * width + col] = area;
  1640. GameMap->setBuildNotSet(row, col, false);
  1641. for (long dir = 0; dir < 4; dir ++) {
  1642. long adjR = row + adjTile[dir][0];
  1643. long adjC = col + adjTile[dir][1];
  1644. if ((adjR >= minRow) && (adjR < maxRow) && (adjC >= minCol) && (adjC < maxCol))
  1645. if (GameMap->getBuildNotSet(adjR, adjC) && (areaMap[adjR * width + adjC] == specialID))
  1646. fillSpecialArea(adjR, adjC, area, specialID);
  1647. }
  1648. return(true);
  1649. }
  1650. //------------------------------------------------------------------------------------------
  1651. bool GlobalMap::fillArea (long row, long col, long area, bool offMap) {
  1652. if ((row < minRow) || (row >= maxRow) || (col < minCol) || (col >= maxCol))
  1653. return(false);
  1654. //---------------------------------------------------------------------
  1655. // If we hit a special (wall/gate/forest) tile, politely stop expanding
  1656. // this area into it...
  1657. if (!blank)
  1658. if (GameMap->getBuildSpecial(row, col))
  1659. return(false);
  1660. //-----------------------------------------------------------------------------
  1661. // If we're processing an offMap area, then stop if we hit a non-offMap cell...
  1662. if (offMap) {
  1663. if (!GameMap->getOffMap(row, col))
  1664. return(false);
  1665. }
  1666. else {
  1667. if (GameMap->getOffMap(row, col))
  1668. return(false);
  1669. }
  1670. GameMap->setBuildNotSet(row, col, false);
  1671. if (!blank) {
  1672. if (!hover && GameMap->getDeepWater(row, col)) {
  1673. //GameMap->setPassable(row, col, false);
  1674. areaMap[row * width + col] = -2;
  1675. return(false);
  1676. }
  1677. if (!offMap && !GameMap->getPassable(row, col)) {
  1678. areaMap[row * width + col] = -2;
  1679. return(false);
  1680. }
  1681. }
  1682. gosASSERT(area != -1);
  1683. areaMap[row * width + col] = area;
  1684. for (long dir = 0; dir < 4; dir ++) {
  1685. long adjR = row + adjTile[dir][0];
  1686. long adjC = col + adjTile[dir][1];
  1687. if ((adjR >= minRow) && (adjR < maxRow) && (adjC >= minCol) && (adjC < maxCol))
  1688. if (GameMap->getBuildNotSet(adjR, adjC))
  1689. fillArea(adjR, adjC, area, offMap);
  1690. }
  1691. return(true);
  1692. }
  1693. //------------------------------------------------------------------------------------------
  1694. void GlobalMap::calcSectorAreas (long sectorR, long sectorC) {
  1695. minRow = sectorR * SECTOR_DIM;
  1696. maxRow = minRow + SECTOR_DIM;
  1697. minCol = sectorC * SECTOR_DIM;
  1698. maxCol = minCol + SECTOR_DIM;
  1699. if (blank) {
  1700. for (long r = minRow; r < maxRow; r++)
  1701. for (long c = minCol; c < maxCol; c++)
  1702. if (GameMap->getBuildNotSet(r, c)) {
  1703. if (fillArea(r, c, numAreas, GameMap->getOffMap(r, c)))
  1704. numAreas++;
  1705. }
  1706. }
  1707. else {
  1708. for (long r = minRow; r < maxRow; r++)
  1709. for (long c = minCol; c < maxCol; c++)
  1710. if (GameMap->getBuildNotSet(r, c)) {
  1711. if (GameMap->getBuildSpecial(r, c)) {
  1712. long specialHere = areaMap[r * width + c];
  1713. gosASSERT(specialHere > -1);
  1714. //-------------------------------------------------
  1715. // List this new area as part of the region covered
  1716. // by this gate or wall or forest...
  1717. if (specialAreas[specialHere].numSubAreas == MAX_SPECIAL_SUB_AREAS)
  1718. Fatal(MAX_SPECIAL_SUB_AREAS, " GlobalMap.calcSectorAreas: too many special subareas ");
  1719. specialAreas[specialHere].subAreas[specialAreas[specialHere].numSubAreas] = numAreas;
  1720. specialAreas[specialHere].numSubAreas++;
  1721. //-------------------
  1722. // Now, fill it in...
  1723. fillSpecialArea(r, c, numAreas++, specialHere);
  1724. }
  1725. else if (fillArea(r, c, numAreas, GameMap->getOffMap(r, c)))
  1726. numAreas++;
  1727. }
  1728. }
  1729. }
  1730. //------------------------------------------------------------------------------------------
  1731. void GlobalMap::calcAreas (void) {
  1732. //----------------------------------------------------------------------
  1733. // Large area maps use -1 and -2 to indicate blocked area. CalcArea will
  1734. // always return -1 for blocked-area cells.
  1735. //-----------------------------------------
  1736. // First, process each sector's area map...
  1737. for (long r = 0; r < sectorHeight; r++)
  1738. for (long c = 0; c < sectorWidth; c++)
  1739. calcSectorAreas(r, c);
  1740. //-----------------------------------------------------------------------
  1741. // NOTE: We allocate one extra area--this is used by the calcPath routine
  1742. // as a "scratch" area in some cases...
  1743. gosASSERT(numAreas <= MAX_GLOBALMAP_AREAS);
  1744. areas = (GlobalMapAreaPtr)systemHeap->Malloc(sizeof(GlobalMapArea) * (numAreas + 1));
  1745. gosASSERT(areas != NULL);
  1746. for (long i = 0; i < (numAreas + 1); i++) {
  1747. areas[i].sectorR = 0;
  1748. areas[i].sectorC = 0;
  1749. areas[i].type = AREA_TYPE_NORMAL;
  1750. areas[i].numDoors = 0;
  1751. areas[i].doors = NULL;
  1752. areas[i].ownerWID = 0;
  1753. areas[i].teamID = -1;
  1754. areas[i].offMap = false;
  1755. areas[i].open = true;
  1756. areas[i].cellsCovered = NULL;
  1757. }
  1758. //-----------------------------------------
  1759. // Set each area's sector row and column...
  1760. for (long sectorR = 0; sectorR < sectorHeight; sectorR++)
  1761. for (long sectorC = 0; sectorC < sectorWidth; sectorC++) {
  1762. minRow = sectorR * SECTOR_DIM;
  1763. maxRow = minRow + SECTOR_DIM;
  1764. minCol = sectorC * SECTOR_DIM;
  1765. maxCol = minCol + SECTOR_DIM;
  1766. for (long r = minRow; r < maxRow; r++)
  1767. for (long c = minCol; c < maxCol; c++) {
  1768. long curArea = areaMap[r * width + c];
  1769. if (curArea > -1) {
  1770. areas[curArea].sectorR = sectorR;
  1771. areas[curArea].sectorC = sectorC;
  1772. }
  1773. }
  1774. }
  1775. }
  1776. //------------------------------------------------------------------------------------------
  1777. void GlobalMap::calcCellsCovered (void) {
  1778. /* for (long r = 0; r < height; r++)
  1779. for (long c = 0; c < width; c++) {
  1780. long area = areaMap[r * width + c];
  1781. if (areas[area].type
  1782. }
  1783. */
  1784. }
  1785. //------------------------------------------------------------------------------------------
  1786. void GlobalMap::calcSpecialTypes (void) {
  1787. //systemHeap->walkHeap();
  1788. for (long i = 0; i < numSpecialAreas; i++) {
  1789. if (specialAreas[i].type == SPECIAL_WALL)
  1790. for (long j = 0; j < specialAreas[i].numSubAreas; j++)
  1791. areas[specialAreas[i].subAreas[j]].type = AREA_TYPE_WALL;
  1792. else if (specialAreas[i].type == SPECIAL_GATE)
  1793. for (long j = 0; j < specialAreas[i].numSubAreas; j++)
  1794. areas[specialAreas[i].subAreas[j]].type = AREA_TYPE_GATE;
  1795. else if (specialAreas[i].type == SPECIAL_LAND_BRIDGE)
  1796. for (long j = 0; j < specialAreas[i].numSubAreas; j++)
  1797. areas[specialAreas[i].subAreas[j]].type = AREA_TYPE_LAND_BRIDGE;
  1798. else if (specialAreas[i].type == SPECIAL_FOREST)
  1799. for (long j = 0; j < specialAreas[i].numSubAreas; j++)
  1800. areas[specialAreas[i].subAreas[j]].type = AREA_TYPE_FOREST;
  1801. }
  1802. for (long r = 0; r < height; r++)
  1803. for (long c = 0; c < width; c++)
  1804. if (GameMap->getOffMap(r, c)) {
  1805. long area = calcArea(r, c);
  1806. if (area > -1)
  1807. areas[area].offMap = true;
  1808. }
  1809. //systemHeap->walkHeap();
  1810. }
  1811. //------------------------------------------------------------------------------------------
  1812. void GlobalMap::beginDoorProcessing (void) {
  1813. //systemHeap->walkHeap();
  1814. doorBuildList = (GlobalMapDoorPtr)systemHeap->Malloc(sizeof(GlobalMapDoor) * MAX_GLOBALMAP_DOORS);
  1815. for (long i = 0; i < MAX_GLOBALMAP_DOORS; i++) {
  1816. doorBuildList[i].row = -1;
  1817. doorBuildList[i].col = -1;
  1818. doorBuildList[i].length = -1;
  1819. doorBuildList[i].open = true;
  1820. doorBuildList[i].teamID = -1;
  1821. doorBuildList[i].numLinks[0] = 0;
  1822. doorBuildList[i].numLinks[1] = 0;
  1823. }
  1824. gosASSERT(doorBuildList != NULL);
  1825. }
  1826. //------------------------------------------------------------------------------------------
  1827. void GlobalMap::addDoor (long area1, long area2, long row, long col, long length, long dir) {
  1828. /*
  1829. if (area1 > area2) {
  1830. long savedArea = area1;
  1831. area1 = area2;
  1832. area2 = savedArea;
  1833. dir = (dir + 2) % 4;
  1834. }
  1835. */
  1836. //-------------------------------------------------------
  1837. // First, make sure the door isn't already in the list...
  1838. bool newDoor = true;
  1839. for (long curDoorIndex = 0; curDoorIndex < numDoors; curDoorIndex++) {
  1840. GlobalMapDoorPtr curDoor = &doorBuildList[curDoorIndex];
  1841. if ((curDoor->row == row) && (curDoor->col == col))
  1842. if ((curDoor->length == length) && (curDoor->direction[0] == dir))
  1843. newDoor = false;
  1844. }
  1845. if (newDoor) {
  1846. /*
  1847. if (area1 > area2) {
  1848. //-----------------------------
  1849. // Smaller area number first...
  1850. long savedArea1 = area1;
  1851. area1 = area2;
  1852. area2 = savedArea1;
  1853. dir = (dir + 2) % 4;
  1854. }
  1855. */
  1856. doorBuildList[numDoors].row = row;
  1857. doorBuildList[numDoors].col = col;
  1858. doorBuildList[numDoors].length = length;
  1859. doorBuildList[numDoors].open = true;
  1860. doorBuildList[numDoors].teamID = -1;
  1861. doorBuildList[numDoors].area[0] = area1;
  1862. doorBuildList[numDoors].areaCost[0] = 1;
  1863. doorBuildList[numDoors].direction[0] = dir;
  1864. doorBuildList[numDoors].area[1] = area2;
  1865. doorBuildList[numDoors].areaCost[1] = 1;
  1866. doorBuildList[numDoors].direction[1] = (dir + 2) % 4;
  1867. numDoors++;
  1868. if (numDoors >= MAX_GLOBALMAP_DOORS)
  1869. Fatal(numDoors, " Too many Global Doors ");
  1870. }
  1871. if (numDoors >= MAX_GLOBALMAP_DOORS)
  1872. STOP(("Too Many Doors %d", numDoors));
  1873. }
  1874. //------------------------------------------------------------------------------------------
  1875. void GlobalMap::endDoorProcessing (void) {
  1876. // systemHeap->walkHeap();
  1877. if (doorBuildList) {
  1878. //-----------------------------------------------------------------------
  1879. // First, save the door list. Note that we make 2 extra doors, to be used
  1880. // by the pathfinder...
  1881. doors = (GlobalMapDoorPtr)systemHeap->Malloc(sizeof(GlobalMapDoor) * (numDoors + NUM_DOOR_OFFSETS));
  1882. memcpy(doors, doorBuildList, sizeof(GlobalMapDoor) * (numDoors + NUM_DOOR_OFFSETS));
  1883. //----------------------------
  1884. // Free the temp build list...
  1885. systemHeap->Free(doorBuildList);
  1886. doorBuildList = NULL;
  1887. }
  1888. // systemHeap->walkHeap();
  1889. }
  1890. //------------------------------------------------------------------------------------------
  1891. long GlobalMap::numAreaDoors (long area) {
  1892. long doorCount = 0;
  1893. for (long doorIndex = 0; doorIndex < numDoors; doorIndex++)
  1894. if ((doors[doorIndex].area[0] == area) || (doors[doorIndex].area[1] == area))
  1895. doorCount++;
  1896. if (doorCount > 255)
  1897. Fatal(doorCount, " Too many area doors ");
  1898. return(doorCount);
  1899. }
  1900. //------------------------------------------------------------------------------------------
  1901. void GlobalMap::getAreaDoors (long area, DoorInfoPtr doorList) {
  1902. long doorCount = 0;
  1903. for (long doorIndex = 0; doorIndex < numDoors; doorIndex++)
  1904. if ((doors[doorIndex].area[0] == area) || (doors[doorIndex].area[1] == area)) {
  1905. doorList[doorCount].doorIndex = doorIndex;
  1906. long doorSide = (doors[doorIndex].area[1] == area);
  1907. doorList[doorCount].doorSide = doorSide;
  1908. doorCount++;
  1909. }
  1910. }
  1911. //------------------------------------------------------------------------------------------
  1912. #define MAX_DOORS_PER_SECTOR 500 // This should be WAY more than we'll ever need...
  1913. long maxNumDoors = 0;
  1914. void GlobalMap::calcGlobalDoors (void) {
  1915. short doorMap[SECTOR_DIM][SECTOR_DIM];
  1916. beginDoorProcessing();
  1917. for (long sectorR = 0; sectorR < sectorHeight; sectorR++) {
  1918. for (long sectorC = 0; sectorC < sectorWidth; sectorC++) {
  1919. for (long dir = 1; dir < 3; dir++) {
  1920. for (long x = 0; x < SECTOR_DIM; x++)
  1921. for (long y = 0; y < SECTOR_DIM; y++)
  1922. doorMap[x][y] = -1;
  1923. minRow = sectorR * SECTOR_DIM;
  1924. maxRow = minRow + SECTOR_DIM;
  1925. minCol = sectorC * SECTOR_DIM;
  1926. maxCol = minCol + SECTOR_DIM;
  1927. //-------------------------------------------
  1928. // First, find all doors in this direction...
  1929. for (long r = minRow; r < maxRow; r++)
  1930. for (long c = minCol; c < maxCol; c++) {
  1931. //-------------------------------------------------------
  1932. // First, check each cell in the sector to see if
  1933. // it's a "door" cell (is open and adjacent to an
  1934. // open cell in another area in the current direction)...
  1935. long curArea = areaMap[r * width + c];
  1936. if (curArea > -1) {
  1937. //---------------------------------------------------------
  1938. // The doorMap tracks, for each tile, what area is adjacent
  1939. // to it thru a door...
  1940. long adjR = r + adjTile[dir][0];
  1941. long adjC = c + adjTile[dir][1];
  1942. if ((adjR >= 0) && (adjR < height) && (adjC >= 0) && (adjC < width)) {
  1943. long adjArea = areaMap[adjR * width + adjC];
  1944. if ((adjArea > -1) && (curArea != adjArea)) {
  1945. AreaType curAreaType = areas[curArea].type;
  1946. AreaType adjAreaType = areas[adjArea].type;
  1947. bool validDoor = false;
  1948. if ((curAreaType == AREA_TYPE_NORMAL) && (adjAreaType == AREA_TYPE_NORMAL))
  1949. validDoor = true;
  1950. if (validDoor)
  1951. doorMap[r - minRow][c - minCol] = adjArea;
  1952. }
  1953. }
  1954. }
  1955. }
  1956. //--------------------------------------------------------------------------
  1957. // Now, process the doors and add them to the global door list. The
  1958. // direction we're currently processing will dictate how to
  1959. // make our sweep across the sector map. So, we have a chunk a code for each
  1960. // direction (THIS MUST BE MODIFIED IF WE INCLUDE DIAGONAL DIRECTIONS WHEN
  1961. // MOVING ON THE GLOBAL MAP)...
  1962. if (dir == 1) {
  1963. for (long c = maxCol - 1; c >= minCol; c--) {
  1964. long r = minRow;
  1965. while (r < maxRow) {
  1966. long adjArea = doorMap[r - minRow][c - minCol];
  1967. if (adjArea > -1) {
  1968. long curArea = areaMap[r * width + c];
  1969. //-----------------------------------
  1970. // We have a door. Calc its length...
  1971. long length = 0;
  1972. while ((r < maxRow) && (areaMap[r * width + c] == curArea) && (doorMap[r - minRow][c - minCol] == adjArea)) {
  1973. length++;
  1974. r++;
  1975. }
  1976. //---------------------------------------
  1977. // Now, add it to the global door list...
  1978. addDoor(curArea, adjArea, r - length, c, length, dir);
  1979. }
  1980. else
  1981. r++;
  1982. }
  1983. }
  1984. }
  1985. else {
  1986. for (long r = maxRow - 1; r >= minRow; r--) {
  1987. long c = minCol;
  1988. while (c < maxCol) {
  1989. long adjArea = doorMap[r - minRow][c - minCol];
  1990. if (adjArea > -1) {
  1991. long curArea = areaMap[r * width + c];
  1992. //-----------------------------------
  1993. // We have a door. Calc its length...
  1994. long length = 0;
  1995. while ((c < maxCol) && (areaMap[r * width + c] == curArea) && (doorMap[r - minRow][c - minCol] == adjArea)) {
  1996. length++;
  1997. c++;
  1998. }
  1999. //-----------------------------------------
  2000. // Now, add it to the sector's door list...
  2001. addDoor(curArea, adjArea, r, c - length, length, dir);
  2002. }
  2003. else
  2004. c++;
  2005. }
  2006. }
  2007. }
  2008. }
  2009. }
  2010. }
  2011. endDoorProcessing();
  2012. }
  2013. //------------------------------------------------------------------------------------------
  2014. void GlobalMap::calcAreaDoors (void) {
  2015. numDoorInfos = 0;
  2016. for (long area = 0; area < numAreas; area++) {
  2017. areas[area].numDoors = numAreaDoors(area);
  2018. numDoorInfos += areas[area].numDoors;
  2019. if (areas[area].numDoors) {
  2020. areas[area].doors = (DoorInfoPtr)systemHeap->Malloc(sizeof(DoorInfo) * areas[area].numDoors);
  2021. getAreaDoors(area, areas[area].doors);
  2022. }
  2023. else
  2024. areas[area].doors = NULL;
  2025. }
  2026. }
  2027. //------------------------------------------------------------------------------------------
  2028. long GlobalMap::calcLinkCost (long startDoor, long thruArea, long goalDoor) {
  2029. if ((doors[startDoor].area[0] != thruArea) && (doors[startDoor].area[1] != thruArea))
  2030. return(-1);
  2031. long startSide = (doors[startDoor].area[1] == thruArea);
  2032. long startRow = doors[startDoor].row;
  2033. long startCol = doors[startDoor].col;
  2034. if (doors[startDoor].direction[0] == 1) {
  2035. //-------------------------------------------------------------------------------
  2036. // Door goes from west to east
  2037. // For now, use the midpoint of the door for purposes of calcing the cost between
  2038. // doors...
  2039. startRow += (doors[startDoor].length / 2);
  2040. startCol += startSide;
  2041. }
  2042. else {
  2043. //------------------------------
  2044. // Door goes from north to south
  2045. // For now, use the midpoint of the door for purposes of calcing the cost between
  2046. // doors...
  2047. startRow += startSide;
  2048. startCol += (doors[startDoor].length / 2);
  2049. }
  2050. if ((doors[goalDoor].area[0] != thruArea) && (doors[goalDoor].area[1] != thruArea))
  2051. return(-2);
  2052. long goalSide = (doors[goalDoor].area[1] == thruArea);
  2053. long goalRow = doors[goalDoor].row;
  2054. long goalCol = doors[goalDoor].col;
  2055. if (doors[goalDoor].direction[0] == 1) {
  2056. //-------------------------------------------------------------------
  2057. // Door goes from west to east. For now, use the midpoint of the door
  2058. // for purposes of calcing the cost between doors...
  2059. goalRow += (doors[goalDoor].length / 2);
  2060. goalCol += goalSide;
  2061. }
  2062. else {
  2063. //------------------------------
  2064. // Door goes from north to south
  2065. goalRow += goalSide;
  2066. goalCol += (doors[goalDoor].length / 2);
  2067. }
  2068. Stuff::Vector3D goalWorldPos;
  2069. land->cellToWorld(goalRow, goalCol, goalWorldPos);
  2070. //goalWorldPos.x = (float)goalCol * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  2071. //goalWorldPos.y = (Terrain::worldUnitsMapSide / 2) - ((float)goalCellR * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  2072. //goalWorldPos.z = (float)0; // How do we get the elevation for this point? Do we care?
  2073. long cost = 9999;
  2074. if (!PathFindMap[SECTOR_PATHMAP]) {
  2075. PathFindMap[SECTOR_PATHMAP] = new MoveMap;
  2076. PathFindMap[SECTOR_PATHMAP]->init(30, 30);
  2077. }
  2078. if (!PathFindMap[SIMPLE_PATHMAP]) {
  2079. PathFindMap[SIMPLE_PATHMAP] = new MoveMap;
  2080. PathFindMap[SIMPLE_PATHMAP]->init(SimpleMovePathRange * 2 + 1, SimpleMovePathRange * 2 + 1);
  2081. }
  2082. //-------------------------------------------------------------------------
  2083. // Let's make sure the bridge tiles are NOT blocked here (since, during the
  2084. // game, they won't be, unless destroyed, in which case we won't care since
  2085. // they're their own area)...
  2086. MovePath newPath;
  2087. if (blank) {
  2088. newPath.numSteps = 10;
  2089. newPath.cost = 10;
  2090. }
  2091. else if ((areas[thruArea].type == AREA_TYPE_WALL) || (areas[thruArea].type == AREA_TYPE_GATE) || (areas[thruArea].type == AREA_TYPE_FOREST)) {
  2092. //---------------------------------------------
  2093. // These costs are for when the area is open...
  2094. newPath.numSteps = 10;
  2095. newPath.cost = 10;
  2096. }
  2097. else {
  2098. ClearBridgeTiles = true;
  2099. long mapULr = areas[thruArea].sectorR * SECTOR_DIM;
  2100. long mapULc = areas[thruArea].sectorC * SECTOR_DIM;
  2101. long moveParams = MOVEPARAM_NONE;
  2102. if (hover)
  2103. moveParams |= (MOVEPARAM_WATER_SHALLOW + MOVEPARAM_WATER_DEEP);
  2104. else if (!blank)
  2105. moveParams |= MOVEPARAM_WATER_SHALLOW;
  2106. #if USE_SEPARATE_WATER_MAPS
  2107. // To REALLY fix the separate water maps, we should do this for vehicles and mechs separately (and vehicles
  2108. // don't use shallow). But, if nothing else, this fixes the bug where we weren't passing in the moveParams.
  2109. PathFindMap[SECTOR_PATHMAP]->setUp(mapULr,
  2110. mapULc,
  2111. SECTOR_DIM,
  2112. SECTOR_DIM,
  2113. hover ? 1 : 0,
  2114. NULL,
  2115. startRow,
  2116. startCol,
  2117. goalWorldPos,
  2118. goalRow - mapULr,
  2119. goalCol - mapULc,
  2120. 10,
  2121. 0,
  2122. 8,
  2123. moveParams);
  2124. #else
  2125. PathFindMap[SECTOR_PATHMAP]->setUp(mapULr,
  2126. mapULc,
  2127. SECTOR_DIM,
  2128. SECTOR_DIM,
  2129. hover ? 1 : 0,
  2130. NULL,
  2131. startRow,
  2132. startCol,
  2133. goalWorldPos,
  2134. goalRow - mapULr,
  2135. goalCol - mapULc,
  2136. 10,
  2137. 0,
  2138. 8,
  2139. MOVEPARAM_NONE);
  2140. #endif
  2141. long goalCell[2];
  2142. PathFindMap[SECTOR_PATHMAP]->calcPath(&newPath, NULL, goalCell);
  2143. ClearBridgeTiles = false;
  2144. }
  2145. //-------------------------------------------------------------------------
  2146. // If there is no path, find out why! In theory, this should never occur...
  2147. if (newPath.numSteps == 0) {
  2148. /*
  2149. File* pathDebugFile = new File;
  2150. pathDebugFile->create("movemap1.dbg");
  2151. PathFindMap->writeDebug(pathDebugFile);
  2152. pathDebugFile->close();
  2153. delete pathDebugFile;
  2154. pathDebugFile = NULL;
  2155. */
  2156. //--------------------------------------------------------------------------
  2157. // If the cost is 1, then our start and goal cells are the same. Thus, there
  2158. // is a path, it's just REALLY short :)
  2159. if (newPath.cost == 1)
  2160. return(1);
  2161. else
  2162. return(9999);
  2163. }
  2164. else
  2165. cost = newPath.getCost();
  2166. //#endif
  2167. gosASSERT(cost != 0);
  2168. return(cost);
  2169. }
  2170. //------------------------------------------------------------------------------------------
  2171. void GlobalMap::changeAreaLinkCost (long area, long cost) {
  2172. GlobalMapAreaPtr thruArea = &areas[area];
  2173. long numDoors = thruArea->numDoors;
  2174. for (long i = 0; i < numDoors; i++) {
  2175. GlobalMapDoorPtr curDoor = &doors[thruArea->doors[i].doorIndex];
  2176. long doorSide = thruArea->doors[i].doorSide;
  2177. for (long j = 0; j < curDoor->numLinks[doorSide]; j++)
  2178. curDoor->links[doorSide][j].cost = cost;
  2179. }
  2180. }
  2181. //------------------------------------------------------------------------------------------
  2182. void GlobalMap::calcDoorLinks (void) {
  2183. numDoorLinks = 0;
  2184. long maxDoors = 0;
  2185. for (long d = 0; d < numDoors; d++) {
  2186. GlobalMapDoorPtr thisDoor = &doors[d];
  2187. for (long s = 0; s < 2; s++) {
  2188. thisDoor->numLinks[s] = 0;
  2189. thisDoor->links[s] = NULL;
  2190. long area = thisDoor->area[s];
  2191. long areaNumDoors = areas[area].numDoors;
  2192. //if (areaNumDoors >= 0) {
  2193. thisDoor->numLinks[s] = areaNumDoors - 1;
  2194. //----------------------------------------------------------------
  2195. // Allocate enough links for all links to this door plus a scratch
  2196. // link used during path calc...
  2197. thisDoor->links[s] = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * (thisDoor->numLinks[s] + NUM_EXTRA_DOOR_LINKS));
  2198. numDoorLinks += (thisDoor->numLinks[s] + NUM_EXTRA_DOOR_LINKS);
  2199. gosASSERT(thisDoor->links[s] != NULL);
  2200. long linkIndex = 0;
  2201. for (long areaDoor = 0; areaDoor < areaNumDoors; areaDoor++) {
  2202. long doorIndex = areas[area].doors[areaDoor].doorIndex;
  2203. GlobalMapDoorPtr curDoor = &doors[doorIndex];
  2204. if (curDoor != thisDoor) {
  2205. thisDoor->links[s][linkIndex].doorIndex = doorIndex;
  2206. thisDoor->links[s][linkIndex].doorSide = (curDoor->area[1] == area);
  2207. thisDoor->links[s][linkIndex].cost = calcLinkCost(d, area, doorIndex);
  2208. thisDoor->links[s][linkIndex].openCost = thisDoor->links[s][linkIndex].cost;
  2209. linkIndex++;
  2210. }
  2211. }
  2212. //}
  2213. if (areaNumDoors > maxDoors)
  2214. maxDoors = areaNumDoors;
  2215. }
  2216. }
  2217. //----------------------------------------
  2218. // Now, set up the start and goal doors...
  2219. doors[numDoors + DOOR_OFFSET_START].numLinks[0] = maxDoors;
  2220. numDoorLinks += (doors[numDoors + DOOR_OFFSET_START].numLinks[0] + NUM_EXTRA_DOOR_LINKS);
  2221. doors[numDoors + DOOR_OFFSET_START].links[0] = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * (doors[numDoors + DOOR_OFFSET_START].numLinks[0] + NUM_EXTRA_DOOR_LINKS));
  2222. doors[numDoors + DOOR_OFFSET_START].numLinks[1] = 0;
  2223. numDoorLinks += (doors[numDoors + DOOR_OFFSET_START].numLinks[1] + NUM_EXTRA_DOOR_LINKS);
  2224. doors[numDoors + DOOR_OFFSET_START].links[1] = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * (doors[numDoors + DOOR_OFFSET_START].numLinks[1] + NUM_EXTRA_DOOR_LINKS));
  2225. doors[numDoors + DOOR_OFFSET_GOAL].numLinks[0] = maxDoors;
  2226. numDoorLinks += (doors[numDoors + DOOR_OFFSET_GOAL].numLinks[0] + NUM_EXTRA_DOOR_LINKS);
  2227. doors[numDoors + DOOR_OFFSET_GOAL].links[0] = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * (doors[numDoors + DOOR_OFFSET_GOAL].numLinks[0] + NUM_EXTRA_DOOR_LINKS));
  2228. doors[numDoors + DOOR_OFFSET_GOAL].numLinks[1] = 0;
  2229. numDoorLinks += (doors[numDoors + DOOR_OFFSET_GOAL].numLinks[1] + NUM_EXTRA_DOOR_LINKS);
  2230. doors[numDoors + DOOR_OFFSET_GOAL].links[1] = (DoorLinkPtr)systemHeap->Malloc(sizeof(DoorLink) * (doors[numDoors + DOOR_OFFSET_GOAL].numLinks[1] + NUM_EXTRA_DOOR_LINKS));
  2231. long numberL = 0;
  2232. for (long i = 0; i < (numDoors + NUM_DOOR_OFFSETS); i++) {
  2233. long numLinks = doors[i].numLinks[0] + NUM_EXTRA_DOOR_LINKS;
  2234. gosASSERT(numLinks >= 2);
  2235. //result = packetFile->writePacket(whichPacket++, (unsigned char*)doors[i].links[0], sizeof(DoorLink) * numLinks);
  2236. //if (result <= 0)
  2237. // Fatal(result, " GlobalMap.write: Unable to write doorLinks packet ");
  2238. numberL += numLinks;
  2239. numLinks = doors[i].numLinks[1] + NUM_EXTRA_DOOR_LINKS;
  2240. gosASSERT(numLinks >= 2);
  2241. //result = packetFile->writePacket(whichPacket++, (unsigned char*)doors[i].links[1], sizeof(DoorLink) * numLinks);
  2242. //if (result <= 0)
  2243. // Fatal(result, " GlobalMap.write: Unable to write doorLinks packet ");
  2244. numberL += numLinks;
  2245. }
  2246. Assert(numberL == numDoorLinks, 0, " HUH ");
  2247. }
  2248. //------------------------------------------------------------------------------------------
  2249. long GlobalMap::getPathCost (long startArea, long goalArea, bool withSpecialAreas, long& confidence, bool calcIt) {
  2250. //------------------------------------------------------------------------
  2251. // This could be cleaned up if we re-save this data, 0 being no path and 1
  2252. // indicating startArea == goalArea.
  2253. if (startArea < 0)
  2254. return(0);
  2255. if (goalArea < 0)
  2256. return(0);
  2257. if (!areas[startArea].open)
  2258. return(0);
  2259. if (!areas[goalArea].open)
  2260. return(0);
  2261. if (startArea == goalArea)
  2262. return(1);
  2263. #if 1
  2264. GlobalPathStep path[MAX_GLOBAL_PATH];
  2265. if (withSpecialAreas)
  2266. useClosedAreas = true;
  2267. else
  2268. useClosedAreas = false;
  2269. long cost = calcPath(startArea, goalArea, path);
  2270. useClosedAreas = false;
  2271. confidence = GLOBAL_CONFIDENCE_GOOD;
  2272. return(cost);
  2273. #else
  2274. calcedPathCost = false;
  2275. unsigned char data = pathCostTable[startArea * numAreas + goalArea];
  2276. if (withSpecialAreas) {
  2277. unsigned char cost = (data & 0x0C) >> 2;
  2278. if (data & GLOBAL_FLAG_SPECIAL_IMPOSSIBLE) {
  2279. cost = 0;
  2280. confidence = GLOBAL_CONFIDENCE_GOOD;
  2281. }
  2282. else if (data & GLOBAL_FLAG_SPECIAL_CALC) {
  2283. if (calcIt) {
  2284. GlobalPathStep path[MAX_GLOBAL_PATH];
  2285. useClosedAreas = true;
  2286. calcPath(startArea, goalArea, path);
  2287. useClosedAreas = false;
  2288. cost = getPathCost(startArea, goalArea, true, confidence, false);
  2289. calcedPathCost = true;
  2290. confidence = GLOBAL_CONFIDENCE_GOOD;
  2291. }
  2292. else
  2293. confidence = GLOBAL_CONFIDENCE_BAD;
  2294. }
  2295. else
  2296. confidence = GLOBAL_CONFIDENCE_GOOD;
  2297. return(cost);
  2298. }
  2299. else {
  2300. unsigned char cost = (data & 0x03);
  2301. if (data & GLOBAL_FLAG_SPECIAL_IMPOSSIBLE) {
  2302. cost = 0;
  2303. confidence = GLOBAL_CONFIDENCE_GOOD;
  2304. }
  2305. else if (data & GLOBAL_FLAG_NORMAL_CLOSES) {
  2306. if (calcIt) {
  2307. GlobalPathStep path[MAX_GLOBAL_PATH];
  2308. calcPath(startArea, goalArea, path);
  2309. cost = getPathCost(startArea, goalArea, false, confidence, false);
  2310. calcedPathCost = true;
  2311. confidence = GLOBAL_CONFIDENCE_GOOD;
  2312. }
  2313. else if (cost)
  2314. confidence = GLOBAL_CONFIDENCE_BAD;
  2315. else
  2316. confidence = GLOBAL_CONFIDENCE_GOOD;
  2317. }
  2318. else if (data & GLOBAL_FLAG_NORMAL_OPENS) {
  2319. if (calcIt) {
  2320. GlobalPathStep path[MAX_GLOBAL_PATH];
  2321. calcPath(startArea, goalArea, path);
  2322. cost = getPathCost(startArea, goalArea, false, confidence, false);
  2323. calcedPathCost = true;
  2324. confidence = GLOBAL_CONFIDENCE_GOOD;
  2325. }
  2326. else
  2327. confidence = GLOBAL_CONFIDENCE_AT_LEAST;
  2328. }
  2329. else
  2330. confidence = GLOBAL_CONFIDENCE_GOOD;
  2331. return(cost);
  2332. }
  2333. return(0);
  2334. #endif
  2335. }
  2336. //---------------------------------------------------------------------------
  2337. #if USE_PATH_COST_TABLE
  2338. void GlobalMap::setPathFlag (long startArea, long goalArea, unsigned char flag, bool set) {
  2339. long index = startArea * numAreas + goalArea;
  2340. pathCostTable[index] &= (flag ^ 0xFF);
  2341. if (set)
  2342. pathCostTable[index] |= flag;
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. long GlobalMap::getPathFlag (long startArea, long goalArea, unsigned char flag) {
  2346. return(pathCostTable[startArea * numAreas + goalArea] & flag);
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void GlobalMap::setPathCost (long startArea, long goalArea, bool withSpecialAreas, unsigned char cost) {
  2350. if (cost > 0) {
  2351. if (cost < 6)
  2352. cost = 1;
  2353. else if (cost < 11)
  2354. cost = 2;
  2355. else
  2356. cost = 3;
  2357. }
  2358. long index = startArea * numAreas + goalArea;
  2359. if (withSpecialAreas) {
  2360. pathCostTable[index] &= 0xF3;
  2361. cost <<= 2;
  2362. }
  2363. else
  2364. pathCostTable[index] &= 0xFC;
  2365. pathCostTable[index] |= cost;
  2366. }
  2367. //------------------------------------------------------------------------------------------
  2368. void GlobalMap::initPathCostTable (void) {
  2369. if (pathCostTable) {
  2370. systemHeap->Free(pathCostTable);
  2371. pathCostTable = NULL;
  2372. }
  2373. long oldNumAreas = numAreas;
  2374. // if (numAreas > 600)
  2375. // numAreas = 600;
  2376. pathCostTable = (unsigned char*)systemHeap->Malloc(numAreas * numAreas);
  2377. gosASSERT(pathCostTable != NULL);
  2378. for (long startArea = 0; startArea < numAreas; startArea++)
  2379. for (goalArea = 0; goalArea < numAreas; goalArea++) {
  2380. setPathCost(startArea, goalArea, false, 0);
  2381. setPathCost(startArea, goalArea, true, 0);
  2382. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_IMPOSSIBLE, false);
  2383. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, true);
  2384. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_OPENS, true);
  2385. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_CLOSES, true);
  2386. }
  2387. closes = false;
  2388. opens = false;
  2389. numAreas = oldNumAreas;
  2390. }
  2391. //------------------------------------------------------------------------------------------
  2392. void GlobalMap::resetPathCostTable (void) {
  2393. if (!pathCostTable)
  2394. return;
  2395. if (!closes && !opens)
  2396. return;
  2397. for (long startArea = 0; startArea < numAreas; startArea++)
  2398. for (goalArea = 0; goalArea < numAreas; goalArea++) {
  2399. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_OPENS, opens);
  2400. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_CLOSES, closes);
  2401. }
  2402. closes = false;
  2403. opens = false;
  2404. }
  2405. //------------------------------------------------------------------------------------------
  2406. void GlobalMap::calcPathCostTable (void) {
  2407. if (pathCostTable) {
  2408. systemHeap->Free(pathCostTable);
  2409. pathCostTable = NULL;
  2410. }
  2411. pathCostTable = (unsigned char*)systemHeap->Malloc(numAreas * numAreas);
  2412. gosASSERT(pathCostTable != NULL);
  2413. for (long i = 0; i < numAreas; i++)
  2414. openArea(i);
  2415. GlobalPathStep globalPath[MAX_GLOBAL_PATH];
  2416. for (long startArea = 0; startArea < numAreas; startArea++)
  2417. for (goalArea = 0; goalArea < numAreas; goalArea++) {
  2418. long numSteps = calcPath(startArea, goalArea, globalPath);
  2419. //----------------------------------------------------------
  2420. // For now, we'll just store the number of area steps in the
  2421. // table. If no path (or start and goal are the same), we'll store 0.
  2422. // 255 is reserved to mark the need to re-calc during the game...
  2423. setPathCost(startArea, goalArea, true, numSteps);
  2424. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_IMPOSSIBLE, false);
  2425. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, true);
  2426. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_OPENS, false);
  2427. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_CLOSES, false);
  2428. }
  2429. for (i = 0; i < numAreas; i++)
  2430. if ((areas[i].type == AREA_TYPE_WALL) || (areas[i].type == AREA_TYPE_GATE) || (areas[i].type == AREA_TYPE_FOREST))
  2431. openArea(i);
  2432. for (startArea = 0; startArea < numAreas; startArea++)
  2433. for (goalArea = 0; goalArea < numAreas; goalArea++) {
  2434. long numSteps = calcPath(startArea, goalArea, globalPath);
  2435. //----------------------------------------------------------
  2436. // For now, we'll just store the number of area steps in the
  2437. // table. If no path (or start and goal are the same), we'll store 0.
  2438. // 255 is reserved to mark the need to re-calc during the game...
  2439. setPathCost(startArea, goalArea, true, numSteps);
  2440. }
  2441. for (i = 0; i < numAreas; i++)
  2442. if ((areas[i].type == AREA_TYPE_WALL) || (areas[i].type == AREA_TYPE_GATE) || (areas[i].type == AREA_TYPE_FOREST))
  2443. closeArea(i);
  2444. closes = false;
  2445. opens = false;
  2446. }
  2447. #endif
  2448. //------------------------------------------------------------------------------------------
  2449. void GlobalMap::clearPathExistsTable (void) {
  2450. long tableSize = numAreas * (numAreas / 4 + 1);
  2451. memset(pathExistsTable, GLOBALPATH_EXISTS_UNKNOWN, tableSize);
  2452. }
  2453. //------------------------------------------------------------------------------------------
  2454. void GlobalMap::setPathExists (long fromArea, long toArea, unsigned char set) {
  2455. if (!pathExistsTable)
  2456. return;
  2457. if (fromArea < 0)
  2458. return;
  2459. if (toArea < 0)
  2460. return;
  2461. long rowWidth = numAreas / 4 + 1;
  2462. unsigned char* pathByte = pathExistsTable;
  2463. pathByte += (rowWidth * fromArea + (toArea / 4));
  2464. unsigned char pathShift = (toArea % 4) * 2;
  2465. unsigned char pathBit = 0x03 << pathShift;
  2466. *pathByte &= (pathBit ^ 0xFF);
  2467. if (set)
  2468. *pathByte |= (set << pathShift);
  2469. }
  2470. //------------------------------------------------------------------------------------------
  2471. unsigned char GlobalMap::getPathExists (long fromArea, long toArea) {
  2472. if (!pathExistsTable)
  2473. return(false);
  2474. if (fromArea < 0)
  2475. return(false);
  2476. if (toArea < 0)
  2477. return(false);
  2478. long rowWidth = numAreas / 4 + 1;
  2479. unsigned char* pathByte = pathExistsTable;
  2480. pathByte += (rowWidth * fromArea + (toArea / 4));
  2481. unsigned char pathShift = (toArea % 4) * 2;
  2482. unsigned char pathBit = 0x03 << pathShift;
  2483. return(*pathByte & pathBit);
  2484. }
  2485. //------------------------------------------------------------------------------------------
  2486. long GlobalMap::exitDirection (long doorIndex, long fromArea) {
  2487. if (doors[doorIndex].area[0] == fromArea)
  2488. return(doors[doorIndex].direction[0]);
  2489. else if (doors[doorIndex].area[1] == fromArea)
  2490. return(doors[doorIndex].direction[1]);
  2491. return(-1);
  2492. }
  2493. //------------------------------------------------------------------------------------------
  2494. void GlobalMap::getDoorTiles (long area, long door, GlobalMapDoorPtr areaDoor) {
  2495. *areaDoor = doors[areas[area].doors[door].doorIndex];
  2496. }
  2497. //------------------------------------------------------------------------------------------
  2498. bool GlobalMap::getAdjacentAreaCell (long area, long adjacentArea, long& cellRow, long& cellCol) {
  2499. for (long i = 0; i < areas[area].numDoors; i++) {
  2500. long doorIndex = areas[area].doors[i].doorIndex;
  2501. long doorSide = areas[area].doors[i].doorSide;
  2502. if (doors[doorIndex].area[doorSide % 1] == adjacentArea) {
  2503. if ((doors[doorIndex].area[0] != area) && (doors[doorIndex].area[1] != area))
  2504. STOP(("bad adjacent area door", 0));
  2505. long goalSide = (doors[doorIndex].area[1] == adjacentArea);
  2506. cellRow = doors[doorIndex].row;
  2507. cellCol = doors[doorIndex].col;
  2508. if (doors[doorIndex].direction[0] == 1) {
  2509. //-------------------------------------------------------------------
  2510. // Door goes from west to east. For now, use the midpoint of the door
  2511. // for purposes of calcing the cost between doors...
  2512. cellRow += (doors[doorIndex].length / 2);
  2513. cellCol += goalSide;
  2514. }
  2515. else {
  2516. //------------------------------
  2517. // Door goes from north to south
  2518. cellRow += goalSide;
  2519. cellCol += (doors[doorIndex].length / 2);
  2520. }
  2521. return(true);
  2522. }
  2523. }
  2524. return(false);
  2525. }
  2526. //------------------------------------------------------------------------------------------
  2527. Stuff::Vector3D GlobalMap::getDoorWorldPos (long area, long door, long* prevGoalCell) {
  2528. long cellR, cellC;
  2529. cellR = prevGoalCell[0];// + adjTile[areas[area].doors[door].direction][0];
  2530. cellC = prevGoalCell[1];// += adjTile[areas[area].doors[door].direction][1];
  2531. Stuff::Vector3D pos;
  2532. pos.Zero();
  2533. pos.x = (float)cellC * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  2534. pos.y = (Terrain::worldUnitsMapSide / 2) - ((float)cellR * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  2535. pos.z = (float)land->getTerrainElevation(pos); // How do we get the elevation for this point? Do we care?
  2536. return(pos);
  2537. }
  2538. //------------------------------------------------------------------------------------------
  2539. long GlobalMap::build (MissionMapCellInfo* mapData) {
  2540. #ifdef _DEBUG
  2541. //systemHeap->walkHeap(false,false,"GlobalMap BAD HEAP1\n");
  2542. #endif
  2543. //--------------------------------
  2544. // Note that h and w are in tiles.
  2545. bool wasBlank = blank;
  2546. init(GameMap->height, GameMap->width);
  2547. blank = wasBlank;
  2548. for (long r = 0; r < height; r++)
  2549. for (long c = 0; c < width; c++)
  2550. GameMap->setBuildNotSet(r, c, true);
  2551. numAreas = 0;
  2552. numSpecialAreas = 0;
  2553. if (specialAreas) {
  2554. systemHeap->Free(specialAreas);
  2555. specialAreas = NULL;
  2556. }
  2557. if (mapData) {
  2558. specialAreas = (GlobalSpecialAreaInfo*)systemHeap->Malloc(sizeof(GlobalSpecialAreaInfo) * MAX_SPECIAL_AREAS);
  2559. if (specialAreas == NULL)
  2560. Fatal(0, " GlobalMap.build: unable to malloc specialAreas ");
  2561. calcSpecialAreas(mapData);
  2562. }
  2563. calcAreas();
  2564. calcCellsCovered();
  2565. calcGlobalDoors();
  2566. calcSpecialTypes();
  2567. calcAreaDoors();
  2568. calcDoorLinks();
  2569. //----------------------------------------------
  2570. // Now, build the path tables for each sector...
  2571. /* for (r = 0; r < tileHeight; r++)
  2572. for (long c = 0; c < tileWidth; c++)
  2573. calcSectorPaths(scenarioMap, r, c);
  2574. */
  2575. #ifdef _DEBUG
  2576. //systemHeap->walkHeap(false,false,"GlobalMap BAD HEAP2\n");
  2577. #endif
  2578. //if (blank)
  2579. // calcPathCostTable();
  2580. if (specialAreas) {
  2581. systemHeap->Free(specialAreas);
  2582. specialAreas = NULL;
  2583. }
  2584. #ifdef DEBUG_GLOBALMAP_BUILD
  2585. if (logEnabled && !blank) {
  2586. char s[256];
  2587. for (long i = 0; i < numDoors; i++) {
  2588. sprintf(s, "door %05d, %s(%d), areas %d & %d", i, doors[i].open ? "opened" : "CLOSED", doors[i].teamID, doors[i].area[0], doors[i].area[1]);
  2589. log->write(s);
  2590. for (long side = 0; side < 2; side++) {
  2591. sprintf(s, " side %d", side);
  2592. log->write(s);
  2593. for (long j = 0; j < doors[i].numLinks[side]; j++) {
  2594. sprintf(s, " link %03d, to door %05d, cost %d",
  2595. j,
  2596. doors[i].links[side][j].doorIndex,
  2597. doors[i].links[side][j].cost);
  2598. log->write(s);
  2599. }
  2600. }
  2601. }
  2602. log->write(" ");
  2603. for (i = 0; i < numAreas; i++) {
  2604. sprintf(s, "area %05d, %s(%d)", i, areas[i].open ? "opened" : "CLOSED", areas[i].teamID);
  2605. log->write(s);
  2606. if (areas[i].ownerWID > 0) {
  2607. sprintf(s, " ownerWID is %d", areas[i].ownerWID);
  2608. log->write(s);
  2609. }
  2610. static char* typeString[] = {
  2611. "normal area",
  2612. "wall area",
  2613. "gate area",
  2614. "land bridge area",
  2615. "forest area"
  2616. };
  2617. sprintf(s, " %s", typeString[areas[i].type]);
  2618. log->write(s);
  2619. for (long d = 0; d < areas[i].numDoors; d++) {
  2620. sprintf(s, " door %03d is %d (%d:%d & %d) ",
  2621. d, areas[i].doors[d].doorIndex, areas[i].doors[d].doorSide,
  2622. doors[areas[i].doors[d].doorIndex].area[0], doors[areas[i].doors[d].doorIndex].area[1]);
  2623. log->write(s);
  2624. }
  2625. }
  2626. log->write(" ");
  2627. }
  2628. #endif
  2629. return(NO_ERR);
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. void GlobalMap::setStartDoor (long startArea) {
  2633. GlobalMapDoorPtr startDoor = &doors[numDoors + DOOR_OFFSET_START];
  2634. startDoor->row = 0;
  2635. startDoor->col = 0;
  2636. startDoor->length = 0;
  2637. startDoor->open = true;
  2638. startDoor->teamID = -1;
  2639. startDoor->area[0] = startArea;
  2640. startDoor->area[1] = startArea;
  2641. startDoor->areaCost[0] = 1;
  2642. startDoor->areaCost[1] = 1;
  2643. startDoor->direction[0] = -1;
  2644. startDoor->direction[1] = -1;
  2645. startDoor->numLinks[0] = areas[startArea].numDoors;
  2646. startDoor->numLinks[1] = 0;
  2647. startDoor->fromAreaIndex = 1;
  2648. for (long curLink = 0; curLink < startDoor->numLinks[0]; curLink++) {
  2649. //---------------------------------------------
  2650. // Point the goal "door" to its area's doors...
  2651. long doorIndex = areas[startArea].doors[curLink].doorIndex;
  2652. long doorSide = areas[startArea].doors[curLink].doorSide;
  2653. GlobalMapDoorPtr curDoor = &doors[areas[startArea].doors[curLink].doorIndex];
  2654. long costSum = 1;
  2655. if (startCell[0] > -1) {
  2656. if (startCell[0] > curDoor->row)
  2657. costSum += (startCell[0] - curDoor->row);
  2658. else
  2659. costSum += (curDoor->row - startCell[0]);
  2660. if (startCell[1] > curDoor->col)
  2661. costSum += (startCell[1] - curDoor->col);
  2662. else
  2663. costSum += (curDoor->col - startCell[1]);
  2664. }
  2665. startDoor->links[0][curLink].doorIndex = doorIndex;
  2666. startDoor->links[0][curLink].doorSide = doorSide;
  2667. startDoor->links[0][curLink].cost = costSum;
  2668. startDoor->links[0][curLink].openCost = costSum;
  2669. //----------------------------------------------------
  2670. // Make sure this area door points to the goal door...
  2671. curDoor->links[doorSide][curDoor->numLinks[doorSide]].doorIndex = numDoors + DOOR_OFFSET_START;
  2672. curDoor->links[doorSide][curDoor->numLinks[doorSide]].doorSide = 0;
  2673. curDoor->links[doorSide][curDoor->numLinks[doorSide]].cost = costSum;
  2674. curDoor->links[doorSide][curDoor->numLinks[doorSide]].openCost = costSum;
  2675. curDoor->numLinks[doorSide]++;
  2676. }
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. void GlobalMap::resetStartDoor (long startArea) {
  2680. GlobalMapDoorPtr startDoor = &doors[numDoors + DOOR_OFFSET_START];
  2681. for (long curLink = 0; curLink < startDoor->numLinks[0]; curLink++) {
  2682. long doorSide = areas[startArea].doors[curLink].doorSide;
  2683. doors[areas[startArea].doors[curLink].doorIndex].numLinks[doorSide]--;
  2684. }
  2685. }
  2686. //---------------------------------------------------------------------------
  2687. void GlobalMap::setAreaTeamID (long area, char teamID) {
  2688. //-----------------------------------------------------------------------
  2689. // NOTE: This assumes there won't be adjacent areas with team alignments.
  2690. // If there are, we should record two teamIDs for each door (to track
  2691. // the teamID for each "side" of the door).
  2692. if (area < 0)
  2693. return;
  2694. GlobalMapAreaPtr curArea = &areas[area];
  2695. curArea->teamID = teamID;
  2696. for (long d = 0; d < curArea->numDoors; d++) {
  2697. doors[curArea->doors[d].doorIndex].teamID = teamID;
  2698. }
  2699. //opens = true;
  2700. }
  2701. //---------------------------------------------------------------------------
  2702. void GlobalMap::setAreaOwnerWID (long area, long objWID) {
  2703. if (area < 0)
  2704. return;
  2705. areas[area].ownerWID = objWID;
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. void GlobalMap::setGoalDoor (long goalArea) {
  2709. if ((goalArea < 0) || (goalArea >= numAreas)) {
  2710. char errMsg[256];
  2711. sprintf(errMsg, " GlobalMap.setGoalDoor: bad goalArea (%d of %d) ", goalArea, numAreas);
  2712. gosASSERT((goalArea >= 0) || (goalArea < numAreas));
  2713. }
  2714. GlobalMapDoorPtr goalDoor = &doors[numDoors + DOOR_OFFSET_GOAL];
  2715. goalSector[0] = areas[goalArea].sectorR;
  2716. goalSector[1] = areas[goalArea].sectorC;
  2717. goalDoor->row = 0;
  2718. goalDoor->col = 0;
  2719. goalDoor->length = 0;
  2720. goalDoor->open = true;
  2721. goalDoor->teamID = -1;
  2722. goalDoor->area[0] = goalArea;
  2723. goalDoor->area[1] = goalArea;
  2724. goalDoor->areaCost[0] = 1;
  2725. goalDoor->areaCost[1] = 1;
  2726. goalDoor->direction[0] = -1;
  2727. goalDoor->direction[1] = -1;
  2728. goalDoor->numLinks[0] = areas[goalArea].numDoors;
  2729. goalDoor->numLinks[1] = 0;
  2730. for (long curLink = 0; curLink < goalDoor->numLinks[0]; curLink++) {
  2731. //---------------------------------------------
  2732. // Point the goal "door" to its area's doors...
  2733. long doorIndex = areas[goalArea].doors[curLink].doorIndex;
  2734. long doorSide = areas[goalArea].doors[curLink].doorSide;
  2735. gosASSERT((doorIndex >= 0) && (doorIndex < (numDoors + NUM_DOOR_OFFSETS)));
  2736. GlobalMapDoorPtr curDoor = &doors[doorIndex];
  2737. long costSum = 1;
  2738. if (goalCell[0] > -1) {
  2739. if (goalCell[0] > curDoor->row)
  2740. costSum += (goalCell[0] - curDoor->row);
  2741. else
  2742. costSum += (curDoor->row - goalCell[0]);
  2743. if (goalCell[1] > curDoor->col)
  2744. costSum += (goalCell[1] - curDoor->col);
  2745. else
  2746. costSum += (curDoor->col - goalCell[1]);
  2747. }
  2748. goalDoor->links[0][curLink].doorIndex = doorIndex;
  2749. goalDoor->links[0][curLink].doorSide = doorSide;
  2750. goalDoor->links[0][curLink].cost = costSum;
  2751. goalDoor->links[0][curLink].openCost = costSum;
  2752. //----------------------------------------------------
  2753. // Make sure this area door points to the goal door...
  2754. curDoor->links[doorSide][curDoor->numLinks[doorSide]].doorIndex = numDoors + DOOR_OFFSET_GOAL;
  2755. curDoor->links[doorSide][curDoor->numLinks[doorSide]].doorSide = 0;
  2756. curDoor->links[doorSide][curDoor->numLinks[doorSide]].cost = costSum;
  2757. curDoor->links[doorSide][curDoor->numLinks[doorSide]].openCost = costSum;
  2758. curDoor->numLinks[doorSide]++;
  2759. }
  2760. }
  2761. //---------------------------------------------------------------------------
  2762. void GlobalMap::resetGoalDoor (long goalArea) {
  2763. GlobalMapDoorPtr goalDoor = &doors[numDoors + DOOR_OFFSET_GOAL];
  2764. for (long curLink = 0; curLink < goalDoor->numLinks[0]; curLink++) {
  2765. long doorSide = areas[goalArea].doors[curLink].doorSide;
  2766. doors[areas[goalArea].doors[curLink].doorIndex].numLinks[doorSide]--;
  2767. }
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. #define AREA_ID_BASE 1000000
  2771. long GlobalMap::calcHPrime (long door) {
  2772. gosASSERT((door > -1) && (door < (numDoors + NUM_DOOR_OFFSETS)));
  2773. long sectorR = (areas[doors[door].area[0]].sectorR + areas[doors[door].area[1]].sectorR) / 2;
  2774. long sectorC = (areas[doors[door].area[0]].sectorC + areas[doors[door].area[1]].sectorC) / 2;
  2775. long sum = 0;
  2776. if (sectorR > goalSector[0])
  2777. sum += (sectorR - goalSector[0]);
  2778. else
  2779. sum += (goalSector[0] - sectorR);
  2780. if (sectorC > goalSector[1])
  2781. sum += (sectorC - goalSector[1]);
  2782. else
  2783. sum += (goalSector[1] - sectorC);
  2784. return(sum);
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. inline void GlobalMap::propogateCost (long door, long cost, long fromAreaIndex, long g) {
  2788. gosASSERT((door >= 0) && (door < (numDoors + NUM_DOOR_OFFSETS)) && ((fromAreaIndex == 0) || (fromAreaIndex == 1)));
  2789. GlobalMapDoorPtr curMapDoor = &doors[door];
  2790. if (curMapDoor->g > (g + cost)) {
  2791. curMapDoor->g = g + cost;
  2792. curMapDoor->fPrime = curMapDoor->g + curMapDoor->hPrime;
  2793. if (curMapDoor->flags & MOVEFLAG_OPEN) {
  2794. long openIndex = openList->find(door);
  2795. if (!openIndex) {
  2796. char s[128];
  2797. sprintf(s, "GlobalMap.propogateCost: Cannot find globalmap door [%d, %d, %d, %d] for change\n", door, cost, fromAreaIndex, g);
  2798. gosASSERT(openIndex != NULL);
  2799. }
  2800. openList->change(door, curMapDoor->fPrime);
  2801. }
  2802. else {
  2803. long toAreaIndex = 1 - fromAreaIndex;
  2804. long numLinks = curMapDoor->numLinks[toAreaIndex];
  2805. for (long curLink = 0; curLink < numLinks; curLink++) {
  2806. long succDoor = curMapDoor->links[toAreaIndex][curLink].doorIndex;
  2807. gosASSERT((succDoor >= 0) && (succDoor < numDoors + NUM_DOOR_OFFSETS));
  2808. GlobalMapDoorPtr succMapDoor = &doors[succDoor];
  2809. long succDoorCost = curMapDoor->links[toAreaIndex][curLink].cost;
  2810. if (useClosedAreas) {
  2811. if ((succMapDoor->teamID > -1) && (succMapDoor->teamID != moverTeamID))
  2812. succDoorCost = 1000;
  2813. }
  2814. else {
  2815. if ((succMapDoor->teamID > -1) && (succMapDoor->teamID != moverTeamID))
  2816. succDoorCost = COST_BLOCKED;
  2817. //--------------------------------------------------
  2818. // Whether the door is even open is another issue...
  2819. if (!succMapDoor->open)
  2820. succDoorCost = COST_BLOCKED;
  2821. }
  2822. long succFromAreaIndex = (succMapDoor->area[1] == curMapDoor->area[toAreaIndex]);
  2823. if ((succMapDoor->open || useClosedAreas) && (succDoorCost < COST_BLOCKED))
  2824. if ((succMapDoor->hPrime != HPRIME_NOT_CALCED)/* && (succMapDoor->hPrime < MaxHPrime)*/) {
  2825. if (door == succMapDoor->parent)
  2826. propogateCost(succDoor, succDoorCost, toAreaIndex, curMapDoor->g);
  2827. else if ((curMapDoor->g + succDoorCost) < succMapDoor->g) {
  2828. succMapDoor->cost = succDoorCost;
  2829. succMapDoor->parent = door;
  2830. succMapDoor->fromAreaIndex = succFromAreaIndex;
  2831. propogateCost(succDoor, succDoorCost, succFromAreaIndex, curMapDoor->g);
  2832. }
  2833. }
  2834. }
  2835. }
  2836. }
  2837. }
  2838. //---------------------------------------------------------------------------
  2839. #ifdef TERRAINEDIT
  2840. #define MAX_GLOBAL_PATH 50
  2841. #endif
  2842. long GlobalMap::calcPath (long startArea,
  2843. long goalArea,
  2844. GlobalPathStepPtr path,
  2845. long startRow,
  2846. long startCol,
  2847. long goalRow,
  2848. long goalCol) {
  2849. #ifdef _DEBUG
  2850. //systemHeap->walkHeap(false,false,"GlobalMap:calc BAD HEAP1\n");
  2851. #endif
  2852. if ((startArea == -1) || (goalArea == -1))
  2853. return(-1);
  2854. startCell[0] = startRow;
  2855. startCell[1] = startCol;
  2856. goalCell[0] = goalRow;
  2857. goalCell[1] = goalCol;
  2858. if (logEnabled) {
  2859. char s[50];
  2860. sprintf(s, " start = %d, goal = %d", startArea, goalArea);
  2861. log->write(s);
  2862. if (useClosedAreas)
  2863. log->write(" USE CLOSED AREAS");
  2864. }
  2865. //----------------------------------------------------------------------
  2866. // Door costs should be set here, if we want to modify them real-time...
  2867. //-------------------------------------------------------------------------
  2868. // We will not limit the search, as the global map should never be so big
  2869. // that we'd WANT to limit the search. If so, time to change the sectorDim,
  2870. // etc...
  2871. //-----------------------------------------------
  2872. // If we haven't already, create the OPEN list...
  2873. if (!openList) {
  2874. openList = new PriorityQueue;
  2875. gosASSERT(openList != NULL);
  2876. openList->init(5000);
  2877. }
  2878. //---------------------------------------------------------------
  2879. // NOTE: The last 6 doors are reserved for use by the pathfinder:
  2880. // numDoors + 0 = startArea
  2881. // numDoors + 1 = goalArea
  2882. // numDoors + 2 thru 4 = doors for "blocked" start area
  2883. const long startDoor = numDoors + DOOR_OFFSET_START;
  2884. const long goalDoor = numDoors + DOOR_OFFSET_GOAL;
  2885. //---------------------------------------------
  2886. // Clear the doors and prep 'em for the calc...
  2887. long initHPrime = ZeroHPrime ? 0 : HPRIME_NOT_CALCED;
  2888. for (long d = 0; d < numDoors + NUM_DOOR_OFFSETS; d++) {
  2889. doors[d].cost = 1;
  2890. doors[d].parent = -1;
  2891. doors[d].fromAreaIndex = -1;
  2892. doors[d].flags = 0;
  2893. doors[d].g = 0;
  2894. doors[d].hPrime = initHPrime;
  2895. doors[d].fPrime = 0;
  2896. }
  2897. setStartDoor(startArea);
  2898. setGoalDoor(goalArea);
  2899. if (areas[startArea].offMap)
  2900. openArea(startArea);
  2901. if (areas[goalArea].offMap)
  2902. openArea(goalArea);
  2903. if (!isGateOpenCallback || !isGateDisabledCallback)
  2904. STOP(("Globalmap.calcPath: NULL gate callback"));
  2905. for (long i = 0; i < numAreas; i++)
  2906. if (areas[i].type == AREA_TYPE_GATE) {
  2907. if ((areas[i].teamID == moverTeamID) || (areas[i].teamID == -1)) {
  2908. if (areas[i].ownerWID > 0) {
  2909. if (isGateDisabledCallback(areas[i].ownerWID))
  2910. closeArea(i);
  2911. else
  2912. openArea(i);
  2913. }
  2914. else
  2915. openArea(i);
  2916. }
  2917. else if (isGateOpenCallback(areas[i].ownerWID))
  2918. openArea(i);
  2919. else
  2920. closeArea(i);
  2921. }
  2922. //-------------------------------------------------------------
  2923. // Start with the area we're in, and process the possible doors
  2924. // we can start thru...
  2925. //-----------------------------------------------
  2926. // Put the START vertex on the empty OPEN list...
  2927. PQNode initialVertex;
  2928. initialVertex.key = 0;
  2929. initialVertex.id = startDoor;
  2930. openList->clear();
  2931. #ifdef _DEBUG
  2932. long insertErr =
  2933. #endif
  2934. openList->insert(initialVertex);
  2935. gosASSERT(insertErr == NO_ERR);
  2936. doors[startDoor].flags |= MOVEFLAG_OPEN;
  2937. //******************
  2938. //THROW THE STARTING LINKS ON THE QUEUE...
  2939. //******************
  2940. bool goalFound = false;
  2941. while (!openList->isEmpty()) {
  2942. //----------------------
  2943. // Grab the best node...
  2944. PQNode bestPQNode;
  2945. openList->remove(bestPQNode);
  2946. long bestDoor = bestPQNode.id;
  2947. GlobalMapDoorPtr bestMapDoor = &doors[bestDoor];
  2948. bestMapDoor->flags &= (MOVEFLAG_OPEN ^ 0xFFFFFFFF);
  2949. long bestDoorG = bestMapDoor->g;
  2950. long fromAreaIndex = bestMapDoor->fromAreaIndex;
  2951. //----------------------------
  2952. // Now, close the best node...
  2953. bestMapDoor->flags |= MOVEFLAG_CLOSED;
  2954. //--------------------------
  2955. // Have we found the goal...
  2956. if (bestDoor == goalDoor) {
  2957. goalFound = true;
  2958. break;
  2959. }
  2960. //-------------------------------------------
  2961. // Now, check the door links for this door...
  2962. long toAreaIndex = 1 - fromAreaIndex;
  2963. long thruArea = bestMapDoor->area[toAreaIndex];
  2964. long numLinks = bestMapDoor->numLinks[toAreaIndex];
  2965. if (logEnabled) {
  2966. char s[50];
  2967. sprintf(s, " thruArea = %d, bestDoor = %d, numLinks = %d", thruArea, bestDoor, numLinks);
  2968. log->write(s);
  2969. }
  2970. for (long curLink = 0; curLink < numLinks; curLink++) {
  2971. //------------------------------------------------------
  2972. // If we want to limit the doors thru which the path may
  2973. // travel, we must check right here...
  2974. //--------------------------
  2975. // Now, process this door...
  2976. long succDoor = bestMapDoor->links[toAreaIndex][curLink].doorIndex;
  2977. gosASSERT((succDoor >= 0) && (succDoor < numDoors + NUM_DOOR_OFFSETS));
  2978. GlobalMapDoorPtr succMapDoor = &doors[succDoor];
  2979. if (logEnabled) {
  2980. char s[50];
  2981. sprintf(s, " %02d) succDoor = %d", curLink, succDoor);
  2982. log->write(s);
  2983. }
  2984. long succDoorCost = bestMapDoor->links[toAreaIndex][curLink].cost;
  2985. //----------------------------------------------------------------------------
  2986. // If this is an aligned door, make it more expensive for unfriendly movers...
  2987. if (useClosedAreas) {
  2988. if (succMapDoor->teamID > -1)
  2989. if (TeamRelations[succMapDoor->teamID][moverTeamID] == RELATION_FRIENDLY)
  2990. succDoorCost = 50;
  2991. if (!succMapDoor->open)
  2992. succDoorCost = 1000;
  2993. }
  2994. else {
  2995. if ((succMapDoor->teamID > -1) && (succMapDoor->teamID != moverTeamID))
  2996. succDoorCost = COST_BLOCKED;
  2997. //--------------------------------------------------
  2998. // Whether the door is even open is another issue...
  2999. if (!succMapDoor->open)
  3000. succDoorCost = COST_BLOCKED;
  3001. }
  3002. if (logEnabled) {
  3003. char s[50];
  3004. sprintf(s, " succDoorCost = %d", succDoorCost);
  3005. log->write(s);
  3006. }
  3007. if (succDoorCost < COST_BLOCKED) {
  3008. if (succMapDoor->hPrime == HPRIME_NOT_CALCED)
  3009. succMapDoor->hPrime = calcHPrime(succDoor);
  3010. //----------------------------------------------------
  3011. // What's our cost to go from START to this SUCCESSOR?
  3012. long succDoorG = bestDoorG + succDoorCost;
  3013. long succFromAreaIndex = (succMapDoor->area[1] == thruArea);
  3014. if (succMapDoor->flags & MOVEFLAG_OPEN) {
  3015. //----------------------------------------------
  3016. // This node is already in the OPEN queue to be
  3017. // be processed. Let's check if we have a better
  3018. // path thru this route...
  3019. if (succDoorG < succMapDoor->g) {
  3020. //----------------------------
  3021. // This new path is cheaper...
  3022. succMapDoor->cost = succDoorCost;
  3023. succMapDoor->parent = bestDoor;
  3024. succMapDoor->fromAreaIndex = succFromAreaIndex;
  3025. succMapDoor->g = succDoorG;
  3026. succMapDoor->fPrime = succDoorG + succMapDoor->hPrime;
  3027. long openIndex = openList->find(succDoor);
  3028. if (!openIndex) {
  3029. char s[128];
  3030. sprintf(s, "GlobalMap.calcPath: Cannot find globalmap door [%d, %d, %d, %d] for change\n", succDoor, curLink, succFromAreaIndex, succDoorCost);
  3031. #ifdef USE_OBJECTS
  3032. DebugOpenList(s);
  3033. #endif
  3034. gosASSERT(openIndex != NULL);
  3035. }
  3036. openList->change(openIndex, succMapDoor->fPrime);
  3037. }
  3038. }
  3039. else if (succMapDoor->flags & MOVEFLAG_CLOSED) {
  3040. //-------------------------------------------------
  3041. // This path may be better than this node's current
  3042. // path. If so, we may have to propogate thru...
  3043. if (succDoorG < succMapDoor->g) {
  3044. //----------------------------------
  3045. // This new path is cheaper. We
  3046. // have to propogate the new cost...
  3047. succMapDoor->cost = succDoorCost;
  3048. succMapDoor->parent = bestDoor;
  3049. succMapDoor->fromAreaIndex = succFromAreaIndex;
  3050. propogateCost(succDoor, succDoorCost, succFromAreaIndex, bestDoorG);
  3051. }
  3052. }
  3053. else {
  3054. //-------------------------------------------------
  3055. // This node is neither OPEN nor CLOSED, so toss it
  3056. // into the OPEN list...
  3057. succMapDoor->cost = succDoorCost;
  3058. succMapDoor->parent = bestDoor;
  3059. succMapDoor->fromAreaIndex = succFromAreaIndex;
  3060. succMapDoor->g = succDoorG;
  3061. succMapDoor->fPrime = succDoorG + succMapDoor->hPrime;
  3062. PQNode succPQNode;
  3063. succPQNode.key = succMapDoor->fPrime;
  3064. succPQNode.id = succDoor;
  3065. #ifdef _DEBUG
  3066. long insertErr =
  3067. #endif
  3068. openList->insert(succPQNode);
  3069. gosASSERT(insertErr == NO_ERR);
  3070. succMapDoor->flags |= MOVEFLAG_OPEN;
  3071. }
  3072. }
  3073. }
  3074. }
  3075. resetStartDoor(startArea);
  3076. resetGoalDoor(goalArea);
  3077. if (areas[startArea].offMap)
  3078. closeArea(startArea);
  3079. if (areas[goalArea].offMap)
  3080. closeArea(goalArea);
  3081. if (goalFound) {
  3082. //-------------------------------------------
  3083. // First, let's count how long the path is...
  3084. long curDoor = goalDoor;
  3085. long numSteps = 1;
  3086. while (curDoor != startDoor) {
  3087. numSteps++;
  3088. curDoor = doors[curDoor].parent;
  3089. }
  3090. //-----------------------------------------------------------------------
  3091. // Number of doors to travel to, including the "goal door". We don't care
  3092. // about the "start door" in the path...
  3093. long numDoors = numSteps - 1;
  3094. gosASSERT((numDoors < MAX_GLOBAL_PATH));
  3095. //-----------------------------
  3096. // Now, let's build the path...
  3097. curDoor = goalDoor;
  3098. long curPathDoor = numDoors;
  3099. //--------------------------------
  3100. // Do the doors leading to goal...
  3101. long costToGoal = 0;
  3102. while (curPathDoor > 0) {
  3103. path[curPathDoor - 1].thruArea = doors[curDoor].area[doors[curDoor].fromAreaIndex];
  3104. path[curPathDoor - 1].goalDoor = curDoor;
  3105. path[curPathDoor - 1].costToGoal = costToGoal;
  3106. costToGoal += doors[curDoor].cost;
  3107. curDoor = doors[curDoor].parent;
  3108. curPathDoor--;
  3109. }
  3110. #ifdef _DEBUG
  3111. //systemHeap->walkHeap(false,false,"GlobalMap:calc BAD HEAP2\n");
  3112. #endif
  3113. #ifdef USE_PATH_COST_TABLE
  3114. if (pathCostTable) {
  3115. setPathCost(startArea, goalArea, useClosedAreas, numDoors);
  3116. if (useClosedAreas) {
  3117. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_IMPOSSIBLE, false);
  3118. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, false);
  3119. }
  3120. else {
  3121. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_OPENS, false);
  3122. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, false);
  3123. }
  3124. }
  3125. #endif
  3126. if (logEnabled) {
  3127. char s[50];
  3128. sprintf(s, " PATH FOUND: %d steps", numDoors);
  3129. log->write(s);
  3130. log->write(" ");
  3131. }
  3132. return(numDoors);
  3133. }
  3134. else {
  3135. #ifdef USE_PATH_COST_TABLE
  3136. if (pathCostTable) {
  3137. setPathCost(startArea, goalArea, useClosedAreas, 0);
  3138. if (useClosedAreas) {
  3139. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_IMPOSSIBLE, true);
  3140. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, false);
  3141. }
  3142. else {
  3143. setPathFlag(startArea, goalArea, GLOBAL_FLAG_NORMAL_OPENS, false);
  3144. setPathFlag(startArea, goalArea, GLOBAL_FLAG_SPECIAL_CALC, false);
  3145. }
  3146. }
  3147. #endif
  3148. if (logEnabled)
  3149. log->write(" NO PATH FOUND");
  3150. }
  3151. if (logEnabled)
  3152. log->write(" ");
  3153. return(0);
  3154. }
  3155. //------------------------------------------------------------------------------------------
  3156. long GlobalMap::calcPath (Stuff::Vector3D start, Stuff::Vector3D goal, GlobalPathStepPtr path) {
  3157. long startR, startC;
  3158. land->worldToCell(start, startR, startC);
  3159. long goalR, goalC;
  3160. land->worldToCell(goal, goalR, goalC);
  3161. long numSteps = calcPath(calcArea(startR, startC), calcArea(goalR, goalC), path, startR, startC, goalR, goalC);
  3162. return(numSteps);
  3163. }
  3164. //---------------------------------------------------------------------------
  3165. void GlobalMap::openDoor (long door) {
  3166. doors[door].cost = 10;
  3167. doors[door].open = true;
  3168. }
  3169. //---------------------------------------------------------------------------
  3170. void GlobalMap::closeDoor (long door) {
  3171. doors[door].open = false;
  3172. }
  3173. //---------------------------------------------------------------------------
  3174. void GlobalMap::closeArea (long area) {
  3175. if (area < 0)
  3176. return;
  3177. GlobalMapAreaPtr closedArea = &areas[area];
  3178. closedArea->open = false;
  3179. for (long d = 0; d < closedArea->numDoors; d++)
  3180. closeDoor(closedArea->doors[d].doorIndex);
  3181. for (long i = 0; i < closedArea->numDoors; i++) {
  3182. GlobalMapDoorPtr curDoor = &doors[closedArea->doors[i].doorIndex];
  3183. long doorSide = closedArea->doors[i].doorSide;
  3184. for (long j = 0; j < curDoor->numLinks[doorSide]; j++)
  3185. curDoor->links[doorSide][j].cost = 1000;
  3186. }
  3187. closes = true;
  3188. }
  3189. //---------------------------------------------------------------------------
  3190. void GlobalMap::openArea (long area) {
  3191. if (area < 0)
  3192. return;
  3193. GlobalMapAreaPtr openedArea = &areas[area];
  3194. openedArea->open = true;
  3195. for (long d = 0; d < openedArea->numDoors; d++) {
  3196. long areaSide1 = doors[openedArea->doors[d].doorIndex].area[0];
  3197. long areaSide2 = doors[openedArea->doors[d].doorIndex].area[1];
  3198. if (!isClosedArea(areaSide1) && !isClosedArea(areaSide2))
  3199. openDoor(openedArea->doors[d].doorIndex);
  3200. }
  3201. for (long i = 0; i < openedArea->numDoors; i++) {
  3202. GlobalMapDoorPtr curDoor = &doors[openedArea->doors[i].doorIndex];
  3203. long doorSide = openedArea->doors[i].doorSide;
  3204. for (long j = 0; j < curDoor->numLinks[doorSide]; j++)
  3205. curDoor->links[doorSide][j].cost = curDoor->links[doorSide][j].openCost;
  3206. }
  3207. opens = true;
  3208. }
  3209. //---------------------------------------------------------------------------
  3210. void GlobalMap::print (char* fileName) {
  3211. if (areaMap)
  3212. return;
  3213. File* debugFile = new File;
  3214. debugFile->create(fileName);
  3215. char outString[500];
  3216. for (long row = 0; row < height; row++) {
  3217. outString[0] = NULL;
  3218. for (long col = 0; col < width; col++) {
  3219. if (areaMap[row * width + col] == -2)
  3220. strcat(outString, ">< ");
  3221. else if (areaMap[row * width + col] == -1)
  3222. strcat(outString, "** ");
  3223. else {
  3224. char chStr[8];
  3225. sprintf(chStr, "%02x ", areaMap[row * width + col]);
  3226. strcat(outString, chStr);
  3227. }
  3228. }
  3229. strcat(outString, "\n");
  3230. debugFile->writeString(outString);
  3231. }
  3232. debugFile->writeString("\n");
  3233. debugFile->close();
  3234. delete debugFile;
  3235. debugFile = NULL;
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void GlobalMap::destroy (void) {
  3239. #ifdef _DEBUG
  3240. // systemHeap->walkHeap(false,false,"GlobalMap BAD HEAP1\n");
  3241. #endif
  3242. if (areaMap) {
  3243. systemHeap->Free(areaMap);
  3244. areaMap = NULL;
  3245. }
  3246. if (areas) {
  3247. if (!doorInfos) {
  3248. for (long i = 0; i < (numAreas + 1); i++) {
  3249. if (areas[i].cellsCovered) {
  3250. systemHeap->Free(areas[i].cellsCovered);
  3251. areas[i].cellsCovered = NULL;
  3252. }
  3253. if (areas[i].doors) {
  3254. systemHeap->Free(areas[i].doors);
  3255. areas[i].doors = NULL;
  3256. }
  3257. }
  3258. }
  3259. systemHeap->Free(areas);
  3260. areas = NULL;
  3261. }
  3262. if (doors) {
  3263. if (!doorLinks) {
  3264. for (long i = 0; i < (numDoors + NUM_DOOR_OFFSETS); i++)
  3265. for (long s = 0; s < 2; s++) {
  3266. if (doors[i].links[s]) {
  3267. systemHeap->Free(doors[i].links[s]);
  3268. doors[i].links[s] = NULL;
  3269. }
  3270. }
  3271. }
  3272. systemHeap->Free(doors);
  3273. doors = NULL;
  3274. }
  3275. if (doorInfos) {
  3276. systemHeap->Free(doorInfos);
  3277. doorInfos = NULL;
  3278. }
  3279. if (doorLinks) {
  3280. systemHeap->Free(doorLinks);
  3281. doorLinks = NULL;
  3282. }
  3283. if (pathExistsTable) {
  3284. systemHeap->Free(pathExistsTable);
  3285. pathExistsTable = NULL;
  3286. }
  3287. #ifdef USE_PATH_COST_TABLE
  3288. if (pathCostTable) {
  3289. systemHeap->Free(pathCostTable);
  3290. pathCostTable = NULL;
  3291. }
  3292. #endif
  3293. }
  3294. //----------------------------------------------------------------------------------
  3295. bool GlobalMap::toggleLog (void) {
  3296. if (!log) {
  3297. GameLog::setup();
  3298. log = GameLog::getNewFile();
  3299. if (!log)
  3300. Fatal(0, " Couldn't create lrmove log ");
  3301. long err = log->open("lrmove.log");
  3302. if (err)
  3303. Fatal(0, " Couldn't open lrmove log ");
  3304. #if 1
  3305. GlobalMapPtr map = GlobalMoveMap[0];
  3306. if (true) {
  3307. char s[256];
  3308. for (long i = 0; i < map->numDoors; i++) {
  3309. sprintf(s, "door %05d, %s(%d), areas %d & %d", i, map->doors[i].open ? "opened" : "CLOSED", map->doors[i].teamID, map->doors[i].area[0], map->doors[i].area[1]);
  3310. log->write(s);
  3311. for (long side = 0; side < 2; side++) {
  3312. sprintf(s, " side %d", side);
  3313. log->write(s);
  3314. for (long j = 0; j < map->doors[i].numLinks[side]; j++) {
  3315. sprintf(s, " link %03d, to door %05d, cost %d",
  3316. j,
  3317. map->doors[i].links[side][j].doorIndex,
  3318. map->doors[i].links[side][j].cost);
  3319. log->write(s);
  3320. }
  3321. }
  3322. }
  3323. log->write(" ");
  3324. for (i = 0; i < map->numAreas; i++) {
  3325. sprintf(s, "area %05d, %s(%d)", i, map->areas[i].open ? "opened" : "CLOSED", map->areas[i].teamID);
  3326. log->write(s);
  3327. if (map->areas[i].ownerWID > 0) {
  3328. sprintf(s, " ownerWID is %d", map->areas[i].ownerWID);
  3329. log->write(s);
  3330. }
  3331. static char* typeString[] = {
  3332. "normal area",
  3333. "wall area",
  3334. "gate area",
  3335. "land bridge area",
  3336. "forest area"
  3337. };
  3338. sprintf(s, " %s", typeString[map->areas[i].type]);
  3339. log->write(s);
  3340. for (long d = 0; d < map->areas[i].numDoors; d++) {
  3341. sprintf(s, " door %03d is %d (%d:%d & %d) ",
  3342. d, map->areas[i].doors[d].doorIndex, map->areas[i].doors[d].doorSide,
  3343. map->doors[map->areas[i].doors[d].doorIndex].area[0], map->doors[map->areas[i].doors[d].doorIndex].area[1]);
  3344. log->write(s);
  3345. }
  3346. }
  3347. log->write(" ");
  3348. }
  3349. #endif
  3350. }
  3351. logEnabled = !logEnabled;
  3352. return(logEnabled);
  3353. }
  3354. //----------------------------------------------------------------------------------
  3355. void GlobalMap::writeLog (char* s) {
  3356. if (log)
  3357. log->write(s);
  3358. }
  3359. //**********************************************************************************
  3360. // MOVE MAP class
  3361. //**********************************************************************************
  3362. void* MoveMap::operator new (size_t ourSize) {
  3363. void* result = systemHeap->Malloc(ourSize);
  3364. return(result);
  3365. }
  3366. //---------------------------------------------------------------------------
  3367. void MoveMap::operator delete (void* us) {
  3368. systemHeap->Free(us);
  3369. }
  3370. //---------------------------------------------------------------------------
  3371. void MoveMap::init (long maxW, long maxH) {
  3372. width = maxWidth = maxW;
  3373. height = maxHeight = maxH;
  3374. long mapByteSize = sizeof(MoveMapNode) * maxWidth * maxHeight;
  3375. map = (MoveMapNodePtr)systemHeap->Malloc(mapByteSize);
  3376. gosASSERT(map != NULL);
  3377. mapRowStartTable = (long*)systemHeap->Malloc(maxHeight * sizeof(long));
  3378. gosASSERT(mapRowStartTable != NULL);
  3379. for (long r = 0; r < maxHeight; r++)
  3380. mapRowStartTable[r] = r * maxWidth;
  3381. mapRowTable = (long*)systemHeap->Malloc(maxHeight * maxWidth * sizeof(long));
  3382. gosASSERT(mapRowTable != NULL);
  3383. mapColTable = (long*)systemHeap->Malloc(maxHeight * maxWidth * sizeof(long));
  3384. gosASSERT(mapColTable != NULL);
  3385. for (r = 0; r < maxHeight; r++)
  3386. for (long c = 0; c < maxWidth; c++) {
  3387. long index = mapRowStartTable[r] + c;
  3388. mapRowTable[index] = r;
  3389. mapColTable[index] = c;
  3390. }
  3391. for (r = 0; r < maxHeight; r++)
  3392. for (long c = 0; c < maxWidth; c++) {
  3393. long mapCellIndex = r * maxWidth + c;
  3394. for (long d = 0; d < NUM_ADJ_CELLS; d++) {
  3395. long indexStart = d * 2;
  3396. long adjRow = r + cellShift[indexStart];
  3397. long adjCol = c + cellShift[indexStart + 1];
  3398. if (inMapBounds(adjRow, adjCol, height, width))
  3399. map[mapCellIndex].adjCells[d] = adjRow * maxWidth + adjCol;
  3400. else
  3401. map[mapCellIndex].adjCells[d] = -1;
  3402. }
  3403. }
  3404. float cellLength = Terrain::worldUnitsPerCell * metersPerWorldUnit;
  3405. for (long i = 0; i < DISTANCE_TABLE_DIM; i++)
  3406. for (long j = 0; j < DISTANCE_TABLE_DIM; j++) {
  3407. distanceFloat[i][j] = agsqrt(i, j) * cellLength;
  3408. distanceLong[i][j] = (long)distanceFloat[i][j];
  3409. }
  3410. clear();
  3411. }
  3412. //---------------------------------------------------------------------------
  3413. void MoveMap::clear (void) {
  3414. long numMapCells = maxWidth * height;
  3415. long initHPrime = ZeroHPrime ? 0 : HPRIME_NOT_CALCED;
  3416. for (long i = 0; i < numMapCells; i++) {
  3417. MoveMapNodePtr node = &map[i];
  3418. //------------------------------------------------
  3419. // DON'T NEED TO SET THIS SINCE IS SET IN SETUP...
  3420. node->cost = COST_BLOCKED;
  3421. node->parent = -1;
  3422. node->flags = 0;
  3423. node->hPrime = initHPrime;
  3424. }
  3425. goal.Zero();
  3426. target.x = -999999.0;
  3427. target.y = -999999.0;
  3428. target.z = -999999.0;
  3429. }
  3430. //---------------------------------------------------------------------------
  3431. void MoveMap::setTarget (Stuff::Vector3D targetPos) {
  3432. target = targetPos;
  3433. }
  3434. //---------------------------------------------------------------------------
  3435. void MoveMap::setStart (Stuff::Vector3D* startPos, long startRow, long startCol) {
  3436. if (startPos)
  3437. start = *startPos;
  3438. else {
  3439. start.x = -999999.0;
  3440. start.y = -999999.0;
  3441. start.z = -999999.0;
  3442. }
  3443. if (startRow == -1) {
  3444. land->worldToCell(*startPos, startR, startC);
  3445. startR -= ULr;
  3446. startC -= ULc;
  3447. }
  3448. else {
  3449. startR = startRow;
  3450. Assert(startRow > -1, 0, " HUH ");
  3451. startC = startCol;
  3452. }
  3453. }
  3454. //---------------------------------------------------------------------------
  3455. void MoveMap::setGoal (Stuff::Vector3D goalPos, long goalRow, long goalCol) {
  3456. goal = goalPos;
  3457. if (goalRow == -1) {
  3458. land->worldToCell(goal, goalR, goalC);
  3459. goalR -= ULr;
  3460. goalC -= ULc;
  3461. }
  3462. else {
  3463. goalR = goalRow;
  3464. goalC = goalCol;
  3465. }
  3466. doorDirection = -1;
  3467. GoalIsDoor = false;
  3468. }
  3469. //---------------------------------------------------------------------------
  3470. inline void adjustMoveMapCellCost (MoveMapNodePtr cell, long costAdj) {
  3471. long cost = cell->cost + costAdj;
  3472. if (cost < 1)
  3473. cost = 1;
  3474. cell->cost = cost;
  3475. }
  3476. //---------------------------------------------------------------------------
  3477. void MoveMap::setGoal (long thruArea, long goalDoor) {
  3478. //------------------------------------------------------------------------------
  3479. // We need to set goalR, goalC for the calcHPrime routine (until we come up with
  3480. // a better one that uses the door range. If the global path's final goal is on
  3481. // the other side of this door, we should select THAT cell of the door...
  3482. goal.x = -999999.0;
  3483. goal.y = -999999.0;
  3484. goal.z = -999999.0;
  3485. GoalIsDoor = true;
  3486. door = goalDoor;
  3487. doorSide = (GlobalMoveMap[moveLevel]->doors[goalDoor].area[1] == thruArea);
  3488. long doorDirTable[4][2] = {
  3489. {-1, -1},
  3490. {1, 3},
  3491. {2, 0},
  3492. {-1, -1}
  3493. };
  3494. long goalDoorDir = GlobalMoveMap[moveLevel]->doors[goalDoor].direction[0];
  3495. gosASSERT((goalDoorDir == 1) || (goalDoorDir == 2));
  3496. doorDirection = doorDirTable[goalDoorDir][doorSide];
  3497. long doorLength = GlobalMoveMap[moveLevel]->doors[goalDoor].length;
  3498. if ((doorDirection == 0) || (doorDirection == 2)) {
  3499. //----------------------------------------------------------------
  3500. // We need to set goalR and goalC for the calcHPrime function used
  3501. // in pathfinding...
  3502. goalR = GlobalMoveMap[moveLevel]->doors[goalDoor].row + doorSide - ULr;
  3503. goalC = GlobalMoveMap[moveLevel]->doors[goalDoor].col + (doorLength / 2) - ULc;
  3504. }
  3505. else if ((doorDirection == 1) || (doorDirection == 3)) {
  3506. //----------------------------------------------------------------
  3507. // We need to set goalR and goalC for the calcHPrime function used
  3508. // in pathfinding...
  3509. goalR = GlobalMoveMap[moveLevel]->doors[goalDoor].row + (doorLength / 2) - ULr;
  3510. goalC = GlobalMoveMap[moveLevel]->doors[goalDoor].col + doorSide - ULc;
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. inline long MoveMap::markGoals (Stuff::Vector3D finalGoal) {
  3515. //--------------------------------------
  3516. // First, mark the blocked goal cells...
  3517. static char doorCellState[1024];
  3518. for (long j = 0; j < GlobalMoveMap[moveLevel]->doors[door].length; j++)
  3519. doorCellState[j] = 1;
  3520. if (blockedDoorCallback)
  3521. (*blockedDoorCallback)(moveLevel, door, &doorCellState[0]);
  3522. //-------------------------------------------------------------------------
  3523. // Ultimately, we should do this conversion once for the finalGoal and then
  3524. // store it in the moveOrders data...
  3525. long finalGoalR, finalGoalC;
  3526. land->worldToCell(finalGoal, finalGoalR, finalGoalC);
  3527. //-----------------------------------------
  3528. // Localize the coords for this move map...
  3529. finalGoalR -= ULr;
  3530. finalGoalC -= ULc;
  3531. long doorLength = GlobalMoveMap[moveLevel]->doors[door].length;
  3532. long numGoalCells = 0;
  3533. if ((doorDirection == 0) || (doorDirection == 2)) {
  3534. //--------------------------------
  3535. // Mark the door cells as goals...
  3536. long cellR = goalR;
  3537. long cellC = goalC - (doorLength / 2);
  3538. bool nextToGoal = false;
  3539. if (doorSide == 0) {
  3540. if (finalGoalR == (cellR + 1))
  3541. // if (!doorCellBlocked(cellR, cellC))
  3542. if ((finalGoalC >= cellC) && (finalGoalC < (cellC + doorLength))) {
  3543. //-----------------------------------------------------
  3544. // Our global goal is on the other side of this door...
  3545. map[cellR * width + finalGoalC].setFlag(MOVEFLAG_GOAL);
  3546. numGoalCells++;
  3547. nextToGoal = true;
  3548. }
  3549. }
  3550. else {
  3551. if (finalGoalR == (cellR - 1))
  3552. // if (!doorCellBlocked(cellR, cellC)
  3553. if ((finalGoalC >= cellC) && (finalGoalC < (cellC + doorLength))) {
  3554. //-----------------------------------------------------
  3555. // Our global goal is on the other side of this door...
  3556. map[cellR * maxWidth + finalGoalC].setFlag(MOVEFLAG_GOAL);
  3557. numGoalCells++;
  3558. nextToGoal = true;
  3559. }
  3560. }
  3561. if (!nextToGoal) {
  3562. long adjCost = clearCost / 2;
  3563. long doorCenter = doorLength / 2;
  3564. long cellIndex = cellR * maxWidth + cellC;
  3565. long curCost = doorCenter * adjCost;
  3566. for (long c = 0; c < doorCenter; c++) {
  3567. if (doorCellState[c]) {
  3568. map[cellIndex].setFlag(MOVEFLAG_GOAL);
  3569. numGoalCells++;
  3570. adjustMoveMapCellCost(&map[cellIndex], curCost);
  3571. }
  3572. cellIndex++;
  3573. curCost -= adjCost;
  3574. }
  3575. cellIndex = cellR * maxWidth + cellC + doorCenter;
  3576. curCost = 0;
  3577. for (c = doorCenter; c < doorLength; c++) {
  3578. if (doorCellState[c]) {
  3579. map[cellIndex].setFlag(MOVEFLAG_GOAL);
  3580. numGoalCells++;
  3581. adjustMoveMapCellCost(&map[cellIndex], curCost);
  3582. }
  3583. cellIndex++;
  3584. curCost += adjCost;
  3585. }
  3586. }
  3587. }
  3588. else if ((doorDirection == 1) || (doorDirection == 3)) {
  3589. long cellR = goalR - (doorLength / 2);
  3590. long cellC = goalC;
  3591. bool nextToGoal = false;
  3592. if (finalGoalC == (cellC + 1 - doorSide * 2))
  3593. if ((finalGoalR >= cellR) && (finalGoalR < (cellR + doorLength))) {
  3594. //-----------------------------------------------------
  3595. // Our global goal is on the other side of this door...
  3596. map[finalGoalR * maxWidth + cellC].setFlag(MOVEFLAG_GOAL);
  3597. numGoalCells++;
  3598. nextToGoal = true;
  3599. }
  3600. if (!nextToGoal) {
  3601. long adjCost = clearCost / 2;
  3602. long doorCenter = doorLength / 2;
  3603. long cellIndex = cellR * maxWidth + cellC;
  3604. long curCost = doorCenter * adjCost;
  3605. for (long r = 0; r < doorCenter; r++) {
  3606. if (doorCellState[r]) {
  3607. map[cellIndex].setFlag(MOVEFLAG_GOAL);
  3608. numGoalCells++;
  3609. adjustMoveMapCellCost(&map[cellIndex], curCost);
  3610. }
  3611. cellIndex += maxWidth;
  3612. curCost -= adjCost;
  3613. }
  3614. curCost = 0;
  3615. cellIndex = (cellR + doorCenter) * maxWidth + cellC;
  3616. for (r = doorCenter; r < doorLength; r++) {
  3617. if (doorCellState[r]) {
  3618. map[cellIndex].setFlag(MOVEFLAG_GOAL);
  3619. numGoalCells++;
  3620. adjustMoveMapCellCost(&map[cellIndex], curCost);
  3621. }
  3622. cellIndex += maxWidth;
  3623. curCost += adjCost;
  3624. }
  3625. }
  3626. }
  3627. // if (numGoalCells == 0)
  3628. // OutputDebugString("No Goal Cells");
  3629. return(numGoalCells);
  3630. }
  3631. //---------------------------------------------------------------------------
  3632. inline long MoveMap::markEscapeGoals (Stuff::Vector3D finalGoal) {
  3633. long numGoalCells = 0;
  3634. //-------------------------------------------------------------------------
  3635. // Ultimately, we should do this conversion once for the finalGoal and then
  3636. // store it in the moveOrders data...
  3637. long finalGoalR, finalGoalC;
  3638. land->worldToCell(finalGoal, finalGoalR, finalGoalC);
  3639. long finalGoalArea = GlobalMoveMap[moveLevel]->calcArea(finalGoalR, finalGoalC);
  3640. //------------------------------------------------------------------------
  3641. // For each tile, mark its cells as valid goals if:
  3642. // 1) the tile's areaId == the areaId of the finalGoal
  3643. // 2) OR, if a LR path exists between the tile and the finalGoal tile
  3644. long cellIndex = 0;
  3645. for (long row = 0; row < height; row++)
  3646. for (long col = 0; col < width; col++) {
  3647. if (GameMap->inBounds(ULr + row, ULc + col)) {
  3648. long curArea = GlobalMoveMap[moveLevel]->calcArea(ULr + row, ULc + col);
  3649. long confidence;
  3650. long numLRSteps = GlobalMoveMap[moveLevel]->getPathCost(curArea, finalGoalArea, false, confidence, true);
  3651. bool validGoal = (numLRSteps > 0);
  3652. if (validGoal)
  3653. map[cellIndex].setFlag(MOVEFLAG_GOAL);
  3654. }
  3655. cellIndex++;
  3656. }
  3657. return(numGoalCells);
  3658. }
  3659. //---------------------------------------------------------------------------
  3660. long MoveMap::setUp (long mapULr,
  3661. long mapULc,
  3662. long mapWidth,
  3663. long mapHeight,
  3664. long level,
  3665. Stuff::Vector3D* startPos,
  3666. long startRow,
  3667. long startCol,
  3668. Stuff::Vector3D goalPos,
  3669. long goalRow,
  3670. long goalCol,
  3671. long clearCellCost,
  3672. long jumpCellCost,
  3673. long offsets,
  3674. unsigned long params) {
  3675. //-----------------------------------------------------------------------------
  3676. // If the map has not been allocated yet, then the tile height and width passed
  3677. // is used as both the max and current dimensions. Otherwise, they are only
  3678. // used as the current dimensions...
  3679. if ((params & MOVEPARAM_JUMP) && (jumpCellCost > 0))
  3680. jumpCellCost = clearCellCost;
  3681. if (!map) {
  3682. init(mapHeight, mapWidth);
  3683. setClearCost(clearCellCost);
  3684. setJumpCost(jumpCellCost, offsets);
  3685. }
  3686. else {
  3687. width = mapWidth;
  3688. height = mapHeight;
  3689. setClearCost(clearCellCost);
  3690. setJumpCost(jumpCellCost, offsets);
  3691. clear();
  3692. }
  3693. thruAreas[0] = -1;
  3694. thruAreas[1] = -1;
  3695. //---------------------------------------------
  3696. // First, gotta set some params for the calc...
  3697. ULr = mapULr;
  3698. ULc = mapULc;
  3699. minRow = 0;
  3700. minCol = 0;
  3701. maxRow = mapHeight - 1;
  3702. maxCol = mapWidth - 1;
  3703. moveLevel = level;
  3704. startRow -= ULr;
  3705. startCol -= ULc;
  3706. Assert(startRow > -1, 0, " huh ");
  3707. setStart(startPos, startRow, startCol);
  3708. if (FindingEscapePath) {
  3709. setGoal(goalPos, goalRow, goalCol);
  3710. }
  3711. else
  3712. setGoal(goalPos, goalRow, goalCol);
  3713. travelOffMap = false;
  3714. cannotEnterOffMap = true;
  3715. if (GameMap->getOffMap(ULr + startRow, ULc + startCol))
  3716. travelOffMap = true;
  3717. if (moverWithdrawing) {
  3718. travelOffMap = true;
  3719. cannotEnterOffMap = false;
  3720. }
  3721. bool followRoads = ((params & MOVEPARAM_FOLLOW_ROADS) != 0);
  3722. bool traverseShallowWater = ((params & (MOVEPARAM_WATER_SHALLOW + MOVEPARAM_WATER_DEEP)) != 0);
  3723. bool traverseDeepWater = ((params & MOVEPARAM_WATER_DEEP) != 0);
  3724. bool avoidMines = true;
  3725. if (params & MOVEPARAM_SWEEP_MINES)
  3726. avoidMines = false;
  3727. //-------------------------------------------------
  3728. // Now that the params are set up, build the map...
  3729. long pathLockCost = clearCost << 3;
  3730. //--------------------------------------------------------------
  3731. // Set the map costs based upon the tiles in the scenario map...
  3732. bool groundMover = ((moveLevel == 0) || (moveLevel == 1));
  3733. for (long cellRow = 0; cellRow < height; cellRow++)
  3734. for (long cellCol = 0; cellCol < width; cellCol++) {
  3735. if (GameMap->inBounds(ULr + cellRow, ULc + cellCol)) {
  3736. MapCellPtr mapCell = GameMap->getCell(ULr + cellRow, ULc + cellCol);
  3737. long moveMapIndex = cellRow * maxWidth + cellCol;
  3738. long cost = clearCost;
  3739. bool offMapCell = mapCell->getOffMap();
  3740. if (offMapCell)
  3741. map[moveMapIndex].setFlag(MOVEFLAG_OFFMAP);
  3742. //-----------------------
  3743. // Tile (terrain) type...
  3744. //long tileType = curTile.getTileType();
  3745. if (offMapCell && !travelOffMap)
  3746. cost = COST_BLOCKED;
  3747. else if (groundMover) {
  3748. if (mapCell->getShallowWater()) {
  3749. if (!traverseShallowWater)
  3750. cost = COST_BLOCKED;
  3751. else if (!mapCell->getPassable())
  3752. cost = COST_BLOCKED;
  3753. }
  3754. else if (mapCell->getDeepWater()) {
  3755. if (!traverseDeepWater)
  3756. cost = COST_BLOCKED;
  3757. else if (!mapCell->getPassable())
  3758. cost = COST_BLOCKED;
  3759. }
  3760. else if (mapCell->getGate())
  3761. {
  3762. long areaID = GlobalMoveMap[moveLevel]->calcArea(ULr + cellRow, ULc + cellCol);
  3763. long teamID = -1;
  3764. if (areaID > -1)
  3765. {
  3766. teamID = GlobalMoveMap[moveLevel]->areas[areaID].teamID;
  3767. //Its possible for the ownerWIDs to be invalid for one or two frames after a quick save.
  3768. // We handle this ok later on!! OwnerWIDs restablish themselves right after the first unpaused update!
  3769. /*
  3770. if (!EditorSave)
  3771. if (GlobalMoveMap[moveLevel]->areas[areaID].ownerWID < 1)
  3772. PAUSE(("Gate has no ownerWID. CellR: %d, CellC: %d, WID: %d, MoveLvl: %d",cellRow,cellCol,GlobalMoveMap[moveLevel]->areas[areaID].ownerWID,moveLevel));
  3773. */
  3774. }
  3775. if (!EditorSave && (areaID > -1) && GlobalMoveMap[moveLevel]->isGateDisabledCallback(GlobalMoveMap[moveLevel]->areas[areaID].ownerWID))
  3776. cost = COST_BLOCKED;
  3777. else if ((teamID > -1) && (TeamRelations[teamID][moverTeamID] != RELATION_FRIENDLY)) {
  3778. if (mapCell->getPassable())
  3779. cost <<= 2;
  3780. else
  3781. cost = COST_BLOCKED;
  3782. }
  3783. else if (mapCell->getRoad() && followRoads)
  3784. cost >>= 2;
  3785. }
  3786. else {
  3787. if (!mapCell->getPassable())
  3788. cost = COST_BLOCKED;
  3789. else if (mapCell->getRoad() && followRoads)
  3790. cost >>= 2;
  3791. }
  3792. if (mapCell->getForest())
  3793. cost += forestCost;
  3794. }
  3795. map[moveMapIndex].cost = cost;
  3796. //---------------------------------------------------------------
  3797. // NOTE: With gates, we may want them to set the cell cost rather
  3798. // than just adjust it. Let's see how they play. Since they're
  3799. // set as an overlay, we'll just treat them as such for now.
  3800. if (mapCell->getPathlock(moveLevel == 2))
  3801. adjustMoveMapCellCost(&map[cellRow * maxWidth + cellCol], pathLockCost);
  3802. }
  3803. }
  3804. if (FindingEscapePath)
  3805. markEscapeGoals(goalPos);
  3806. else
  3807. map[goalR * maxWidth + goalC].setFlag(MOVEFLAG_GOAL);
  3808. __int64 startTime = GetCycles();
  3809. if (params & MOVEPARAM_STATIONARY_MOVERS)
  3810. if (placeStationaryMoversCallback)
  3811. (*placeStationaryMoversCallback)(this);
  3812. #ifdef LAB_ONLY
  3813. MCTimeCalcPath1Update += (GetCycles() - startTime);
  3814. #endif
  3815. return(NO_ERR);
  3816. }
  3817. //---------------------------------------------------------------------------
  3818. long MoveMap::setUp (long level,
  3819. Stuff::Vector3D* startPos,
  3820. long startRow,
  3821. long startCol,
  3822. long thruArea[2],
  3823. long goalDoor,
  3824. Stuff::Vector3D finalGoal,
  3825. long clearCellCost,
  3826. long jumpCellCost,
  3827. long offsets,
  3828. unsigned long params) {
  3829. __int64 startTime = GetCycles();
  3830. //-----------------------------------------------------------------------------
  3831. // If the map has not been allocated yet, then the tile height and width passed
  3832. // is used as both the max and current dimensions. Otherwise, they are only
  3833. // used as the current dimensions...
  3834. if (!map)
  3835. init(SECTOR_DIM*2, SECTOR_DIM*2);
  3836. else {
  3837. width = SECTOR_DIM*2;
  3838. height = SECTOR_DIM*2;
  3839. clear();
  3840. }
  3841. thruAreas[0] = thruArea[0];
  3842. thruAreas[1] = thruArea[1];
  3843. //---------------------------------------------
  3844. // First, gotta set some params for the calc...
  3845. long firstSectorRow = GlobalMoveMap[level]->areas[thruArea[0]].sectorR;
  3846. long firstSectorCol = GlobalMoveMap[level]->areas[thruArea[0]].sectorC;
  3847. if (thruArea[1] == -1) {
  3848. ULr = firstSectorRow * SECTOR_DIM;
  3849. ULc = firstSectorCol * SECTOR_DIM;
  3850. minRow = 0;
  3851. minCol = 0;
  3852. maxRow = SECTOR_DIM - 1;
  3853. maxCol = SECTOR_DIM - 1;
  3854. }
  3855. else {
  3856. long secondSectorRow = GlobalMoveMap[level]->areas[thruArea[1]].sectorR;
  3857. long secondSectorCol = GlobalMoveMap[level]->areas[thruArea[1]].sectorC;
  3858. if (secondSectorRow > firstSectorRow) {
  3859. if (secondSectorCol == firstSectorCol) {
  3860. ULr = firstSectorRow * SECTOR_DIM;
  3861. ULc = firstSectorCol * SECTOR_DIM;
  3862. minRow = 0;
  3863. minCol = 0;
  3864. maxRow = SECTOR_DIM * 2 - 1;
  3865. maxCol = SECTOR_DIM - 1;
  3866. }
  3867. else
  3868. STOP(("MoveMap.setup: diagonal sectors not allowed #1"));
  3869. }
  3870. else if (secondSectorRow < firstSectorRow) {
  3871. if (secondSectorCol == firstSectorCol) {
  3872. ULr = secondSectorRow * SECTOR_DIM;
  3873. ULc = secondSectorCol * SECTOR_DIM;
  3874. minRow = 0;
  3875. minCol = 0;
  3876. maxRow = SECTOR_DIM * 2 - 1;
  3877. maxCol = SECTOR_DIM - 1;
  3878. }
  3879. else
  3880. STOP(("MoveMap.setup: diagonal sectors not allowed #2"));
  3881. }
  3882. else {
  3883. if (secondSectorCol > firstSectorCol) {
  3884. ULr = firstSectorRow * SECTOR_DIM;
  3885. ULc = firstSectorCol * SECTOR_DIM;
  3886. minRow = 0;
  3887. minCol = 0;
  3888. maxRow = SECTOR_DIM - 1;
  3889. maxCol = SECTOR_DIM * 2 - 1;
  3890. }
  3891. else if (secondSectorCol < firstSectorCol) {
  3892. ULr = secondSectorRow * SECTOR_DIM;
  3893. ULc = secondSectorCol * SECTOR_DIM;
  3894. minRow = 0;
  3895. minCol = 0;
  3896. maxRow = SECTOR_DIM - 1;
  3897. maxCol = SECTOR_DIM * 2 - 1;
  3898. }
  3899. else {
  3900. ULr = firstSectorRow * SECTOR_DIM;
  3901. ULc = firstSectorCol * SECTOR_DIM;
  3902. minRow = 0;
  3903. minCol = 0;
  3904. maxRow = SECTOR_DIM - 1;
  3905. maxCol = SECTOR_DIM - 1;
  3906. //STOP(("MoveMap.setup: same sectors not allowed"));
  3907. }
  3908. }
  3909. }
  3910. moveLevel = level;
  3911. setClearCost(clearCellCost);
  3912. if ((params & MOVEPARAM_JUMP) && (jumpCellCost > 0))
  3913. jumpCellCost = clearCellCost;
  3914. setJumpCost(jumpCellCost, offsets);
  3915. startRow -= ULr;
  3916. startCol -= ULc;
  3917. setStart(startPos, startRow, startCol);
  3918. if (thruArea[1] == -1)
  3919. setGoal(thruArea[0], goalDoor);
  3920. else
  3921. setGoal(thruArea[1], goalDoor);
  3922. long pathLockCost = clearCost << 3;
  3923. travelOffMap = false;
  3924. cannotEnterOffMap = true;
  3925. if (GameMap->getOffMap(ULr + startRow, ULc + startCol))
  3926. travelOffMap = true;
  3927. if (moverWithdrawing) {
  3928. travelOffMap = true;
  3929. cannotEnterOffMap = false;
  3930. }
  3931. //-------------------------------------------------
  3932. // Now that the params are set up, build the map...
  3933. bool followRoads = ((params & MOVEPARAM_FOLLOW_ROADS) != 0);
  3934. bool traverseShallowWater = ((params & (MOVEPARAM_WATER_SHALLOW + MOVEPARAM_WATER_DEEP)) != 0);
  3935. bool traverseDeepWater = ((params & MOVEPARAM_WATER_DEEP) != 0);
  3936. bool avoidMines = true;
  3937. if (params & MOVEPARAM_SWEEP_MINES)
  3938. avoidMines = false;
  3939. //--------------------------------------------------------------
  3940. // Set the map costs based upon the tiles in the scenario map...
  3941. bool groundMover = ((moveLevel == 0) || (moveLevel == 1));
  3942. for (long cellRow = 0; cellRow < height; cellRow++)
  3943. for (long cellCol = 0; cellCol < width; cellCol++) {
  3944. if (GameMap->inBounds(ULr + cellRow, ULc + cellCol) && inBounds(cellRow, cellCol)) {
  3945. MapCellPtr mapCell = GameMap->getCell(ULr + cellRow, ULc + cellCol);
  3946. long moveMapIndex = cellRow * maxWidth + cellCol;
  3947. long cost = clearCost;
  3948. bool offMapCell = mapCell->getOffMap();
  3949. if (offMapCell)
  3950. map[moveMapIndex].setFlag(MOVEFLAG_OFFMAP);
  3951. long areaID = GlobalMoveMap[moveLevel]->calcArea(ULr + cellRow, ULc + cellCol);
  3952. if (CullPathAreas && (areaID != thruAreas[0]) && (areaID != thruAreas[1]))
  3953. cost = COST_BLOCKED;
  3954. else if (offMapCell && !travelOffMap)
  3955. cost = COST_BLOCKED;
  3956. else if (groundMover) {
  3957. if (mapCell->getShallowWater()) {
  3958. if (!traverseShallowWater)
  3959. cost = COST_BLOCKED;
  3960. else if (!mapCell->getPassable())
  3961. cost = COST_BLOCKED;
  3962. }
  3963. else if (mapCell->getDeepWater()) {
  3964. if (!traverseDeepWater)
  3965. cost = COST_BLOCKED;
  3966. else if (!mapCell->getPassable())
  3967. cost = COST_BLOCKED;
  3968. }
  3969. else if (mapCell->getGate()) {
  3970. long areaID = GlobalMoveMap[moveLevel]->calcArea(ULr + cellRow, ULc + cellCol);
  3971. long teamID = -1;
  3972. if (areaID > -1) {
  3973. teamID = GlobalMoveMap[moveLevel]->areas[areaID].teamID;
  3974. /* See Above
  3975. if (!EditorSave)
  3976. if (GlobalMoveMap[moveLevel]->areas[areaID].ownerWID < 1)
  3977. PAUSE(("Gate has no ownerWID"));
  3978. */
  3979. }
  3980. if (!EditorSave && (areaID > -1) && GlobalMoveMap[moveLevel]->isGateDisabledCallback(GlobalMoveMap[moveLevel]->areas[areaID].ownerWID))
  3981. cost = COST_BLOCKED;
  3982. else if ((teamID > -1) && (TeamRelations[teamID][moverTeamID] != RELATION_FRIENDLY)) {
  3983. if (mapCell->getPassable())
  3984. cost <<= 2;
  3985. else
  3986. cost = COST_BLOCKED;
  3987. }
  3988. else if (mapCell->getRoad() && followRoads)
  3989. cost >>= 2;
  3990. }
  3991. else {
  3992. if (!mapCell->getPassable())
  3993. cost = COST_BLOCKED;
  3994. else if (mapCell->getRoad() && followRoads)
  3995. cost >>= 2;
  3996. }
  3997. if (mapCell->getForest())
  3998. cost += forestCost;
  3999. }
  4000. setCost(cellRow, cellCol, cost);
  4001. if (mapCell->getPathlock(moveLevel == 2))
  4002. adjustMoveMapCellCost(&map[cellRow * maxWidth + cellCol], pathLockCost);
  4003. }
  4004. }
  4005. if (markGoals(finalGoal) == 0)
  4006. return(-1);
  4007. if (params & MOVEPARAM_STATIONARY_MOVERS)
  4008. if (placeStationaryMoversCallback)
  4009. (*placeStationaryMoversCallback)(this);
  4010. #ifdef LAB_ONLY
  4011. MCTimeCalcPath2Update += (GetCycles() - startTime);
  4012. #endif
  4013. return(NO_ERR);
  4014. }
  4015. //---------------------------------------------------------------------------
  4016. inline bool MoveMap::adjacentCellOpen (long mapCellIndex, long dir) {
  4017. long adjCellIndex = map[mapCellIndex].adjCells[dir];
  4018. if (adjCellIndex == -1)
  4019. return(false);
  4020. if (map[adjCellIndex].flags & MOVEFLAG_MOVER_HERE)
  4021. return(false);
  4022. #ifdef USE_MINES_IN_MC2
  4023. if (moverRelation == RELATION_ENEMY) {
  4024. if (GameMap->getTile(adjRow,adjCol).getClanMineMove())
  4025. return(false);
  4026. }
  4027. else {
  4028. if (GameMap->getTile(adjRow,adjCol).getInnerSphereMineMove())
  4029. return(false);
  4030. }
  4031. #endif
  4032. return(map[adjCellIndex].cost < COST_BLOCKED);
  4033. }
  4034. //---------------------------------------------------------------------------
  4035. inline bool MoveMap::adjacentCellOpenJUMP (long r, long c, long dir) {
  4036. long indexStart = dir * 2;
  4037. long adjRow = r + cellShift[indexStart];
  4038. long adjCol = c + cellShift[indexStart + 1];
  4039. if (!inMapBounds(adjRow, adjCol, height, width))
  4040. return(false);
  4041. if (map[adjRow * maxWidth + adjCol].flags & MOVEFLAG_MOVER_HERE)
  4042. return(false);
  4043. #ifdef USE_MINES_IN_MC2
  4044. if (moverRelation == RELATION_ENEMY) {
  4045. if (GameMap->getTile(adjRow,adjCol).getClanMineMove())
  4046. return(false);
  4047. }
  4048. else {
  4049. if (GameMap->getTile(adjRow,adjCol).getInnerSphereMineMove())
  4050. return(false);
  4051. }
  4052. #endif
  4053. return(map[adjRow * maxWidth + adjCol].cost < COST_BLOCKED);
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. inline long MoveMap::calcHPrime (long r, long c) {
  4057. __int64 startTime = GetCycles();
  4058. long sum = 0;
  4059. if (r > goalR)
  4060. sum += (r - goalR);
  4061. else
  4062. sum += (goalR - r);
  4063. if (c > goalC)
  4064. sum += (c - goalC);
  4065. else
  4066. sum += (goalC - c);
  4067. #ifdef LAB_ONLY
  4068. MCTimeCalcPath3Update += (GetCycles() - startTime);
  4069. #endif
  4070. return(sum);
  4071. }
  4072. #define MAX_MAPWIDTH 1000
  4073. //---------------------------------------------------------------------------
  4074. inline void MoveMap::propogateCost (long mapCellIndex, long cost, long g) {
  4075. gosASSERT(cost > 0);
  4076. gosASSERT(g >= 0);
  4077. MoveMapNodePtr curMapNode = &map[mapCellIndex];
  4078. if (curMapNode->g > (g + cost)) {
  4079. curMapNode->cost = cost;
  4080. curMapNode->g = g + cost;
  4081. curMapNode->fPrime = curMapNode->g + curMapNode->hPrime;
  4082. if (curMapNode->flags & MOVEFLAG_OPEN) {
  4083. long openIndex = openList->find(mapCellIndex);
  4084. if (!openIndex) {
  4085. char s[128];
  4086. sprintf(s, "MoveMap.propogateCost: Cannot find movemap node [%d] for change\n", mapCellIndex);
  4087. gosASSERT(openIndex != NULL);
  4088. }
  4089. else
  4090. openList->change(openIndex, curMapNode->fPrime);
  4091. }
  4092. else {
  4093. for (long dir = 0; dir < numOffsets; dir++) {
  4094. //------------------------------------------------------------
  4095. // First, make sure this is a legit direction to go. We do NOT
  4096. // want to clip corners, so we'll check that here...
  4097. bool isDiagonalWalk = IsDiagonalStep[dir];
  4098. if (isDiagonalWalk) {
  4099. // MINE CHECK should go in these adj tests...
  4100. bool adj1Open = false;
  4101. long adjCellIndex = map[mapCellIndex].adjCells[StepAdjDir[dir]];
  4102. if (adjCellIndex > -1)
  4103. if ((map[adjCellIndex].flags & MOVEFLAG_MOVER_HERE) == 0)
  4104. adj1Open = (map[adjCellIndex].cost < COST_BLOCKED);
  4105. bool adj2Open = false;
  4106. adjCellIndex = map[mapCellIndex].adjCells[StepAdjDir[dir + 1]];
  4107. if (adjCellIndex > -1)
  4108. if ((map[adjCellIndex].flags & MOVEFLAG_MOVER_HERE) == 0)
  4109. adj2Open = (map[adjCellIndex].cost < COST_BLOCKED);
  4110. if (!adj1Open && !adj2Open)
  4111. continue;
  4112. //if (!adjacentCellOpen(mapCellIndex, StepAdjDir[dir]) && !adjacentCellOpen(mapCellIndex, StepAdjDir[dir + 1]))
  4113. // continue;
  4114. }
  4115. //----------------------------------------------------------
  4116. // Calc the cell we're checking, offset from current cell...
  4117. long succCellIndex = curMapNode->adjCells[dir];
  4118. //--------------------------------
  4119. // If it's on the map, check it...
  4120. if (succCellIndex > -1) {
  4121. MoveMapNodePtr succMapNode = &map[succCellIndex];
  4122. if (succMapNode->cost < COST_BLOCKED)
  4123. if ((succMapNode->hPrime != HPRIME_NOT_CALCED) && (succMapNode->hPrime < MaxHPrime)) {
  4124. char dirToParent = reverseShift[dir];
  4125. long cost = succMapNode->cost;
  4126. //------------------------------------
  4127. // Diagonal movement is more costly...
  4128. gosASSERT(cost > 0);
  4129. if (isDiagonalWalk)
  4130. cost += (cost << 1);
  4131. if (dirToParent == succMapNode->parent)
  4132. propogateCost(succCellIndex, cost, curMapNode->g);
  4133. else if ((curMapNode->g + cost) < succMapNode->g) {
  4134. succMapNode->parent = dirToParent;
  4135. propogateCost(succCellIndex, cost, curMapNode->g);
  4136. }
  4137. }
  4138. }
  4139. }
  4140. }
  4141. }
  4142. }
  4143. //---------------------------------------------------------------------------
  4144. inline void MoveMap::propogateCostJUMP (long r, long c, long cost, long g) {
  4145. gosASSERT(cost > 0);
  4146. gosASSERT(g >= 0);
  4147. MoveMapNodePtr curMapNode = &map[r * maxWidth + c];
  4148. if (curMapNode->g > (g + cost)) {
  4149. curMapNode->cost = cost;
  4150. curMapNode->g = g + cost;
  4151. curMapNode->fPrime = curMapNode->g + curMapNode->hPrime;
  4152. if (curMapNode->flags & MOVEFLAG_OPEN) {
  4153. long openIndex = openList->find(r * MAX_MAPWIDTH + c);
  4154. if (!openIndex) {
  4155. char s[128];
  4156. sprintf(s, "MoveMap.propogateCost: Cannot find movemap node [%d, %d, %d] for change\n", r, c, r * MAX_MAPWIDTH + c);
  4157. gosASSERT(openIndex != NULL);
  4158. }
  4159. else
  4160. openList->change(openIndex, curMapNode->fPrime);
  4161. }
  4162. else {
  4163. long cellOffsetIndex = 0;
  4164. for (long dir = 0; dir < numOffsets; dir++) {
  4165. //------------------------------------------------------------
  4166. // First, make sure this is a legit direction to go. We do NOT
  4167. // want to clip corners, so we'll check that here...
  4168. bool isDiagonalWalk = IsDiagonalStep[dir];
  4169. if (isDiagonalWalk) {
  4170. if (!adjacentCellOpenJUMP(r, c, StepAdjDir[dir]) && !adjacentCellOpenJUMP(r, c, StepAdjDir[dir + 1])) {
  4171. cellOffsetIndex += 2;
  4172. continue;
  4173. }
  4174. }
  4175. //----------------------------------------------------------
  4176. // Calc the cell we're checking, offset from current cell...
  4177. long succRow = r + cellShift[cellOffsetIndex++];
  4178. long succCol = c + cellShift[cellOffsetIndex++];
  4179. //--------------------------------
  4180. // If it's on the map, check it...
  4181. if (inMapBounds(succRow, succCol, height, width)) {
  4182. MoveMapNodePtr succMapNode = &map[succRow * maxWidth + succCol];
  4183. if (succMapNode->cost < COST_BLOCKED)
  4184. if ((succMapNode->hPrime != HPRIME_NOT_CALCED) && (succMapNode->hPrime < MaxHPrime)) {
  4185. char dirToParent = reverseShift[dir];
  4186. bool jumping = false;
  4187. long cost = succMapNode->cost;
  4188. //------------------------------------
  4189. // Diagonal movement is more costly...
  4190. gosASSERT(cost > 0);
  4191. if (dir > 7) {
  4192. jumping = true;
  4193. if (JumpOnBlocked)
  4194. cost = jumpCost;
  4195. else
  4196. cost += jumpCost;
  4197. }
  4198. else if (isDiagonalWalk)
  4199. cost += (cost / 2);
  4200. if (dirToParent == succMapNode->parent)
  4201. propogateCostJUMP(succRow, succCol, cost, curMapNode->g);
  4202. else if ((curMapNode->g + cost) < succMapNode->g) {
  4203. succMapNode->parent = dirToParent;
  4204. propogateCostJUMP(succRow, succCol, cost, curMapNode->g);
  4205. }
  4206. }
  4207. }
  4208. }
  4209. }
  4210. }
  4211. }
  4212. //---------------------------------------------------------------------------
  4213. //#define DEBUG_PATH
  4214. //#define BLOCKED_PATH_TEST
  4215. //#define TIME_PATH
  4216. //#define DEBUG_MOVE_MAP
  4217. long MoveMap::calcPath (MovePathPtr path, Stuff::Vector3D* goalWorldPos, long* goalCell) {
  4218. #ifdef TIME_PATH
  4219. L_INTEGER calcStart, calcStop;
  4220. QueryPerformanceCounter(calcStart);
  4221. #endif
  4222. if ((goalR < 0) || (goalR >= height) || (goalC < 0) || (goalC >= width)) {
  4223. Stuff::Vector3D p;
  4224. p.x = (float)(goalC) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4225. p.y = (Terrain::worldUnitsMapSide / 2) - ((float)(goalR) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4226. p.z = (float)0; // How do we get the elevation for this point? Do we care?
  4227. char msg[200];
  4228. sprintf(msg, " Bad Move Goal: %d [%d(%d), %d(%d)], (%.2f, %.2f, %.2f)", DebugMovePathType, goalR, height, goalC, width, p.x, p.y, p.z);
  4229. gosASSERT((goalR >= 0) && (goalR < height) && (goalC >= 0) && (goalC < width));
  4230. }
  4231. //------------------------------------------------------------------
  4232. // Let's use their hPrime as a barrier for cutting off the search...
  4233. MaxHPrime = calcHPrime(startR, startC) * 2.5;
  4234. if (MaxHPrime < 500)
  4235. MaxHPrime = 500;
  4236. //-----------------------------------------------
  4237. // If we haven't already, create the OPEN list...
  4238. if (!openList) {
  4239. openList = new PriorityQueue;
  4240. gosASSERT(openList != NULL);
  4241. openList->init(5000);
  4242. }
  4243. long curCol = startC;
  4244. long curRow = startR;
  4245. MoveMapNodePtr curMapNode = &map[mapRowStartTable[curRow] + curCol];
  4246. curMapNode->g = 0;
  4247. if (!ZeroHPrime)
  4248. curMapNode->hPrime = calcHPrime(curRow, curCol);
  4249. curMapNode->fPrime = curMapNode->hPrime;
  4250. //-----------------------------------------------
  4251. // Put the START vertex on the empty OPEN list...
  4252. PQNode initialVertex;
  4253. initialVertex.key = curMapNode->fPrime;
  4254. initialVertex.id = mapRowStartTable[curRow] + curCol;
  4255. initialVertex.row = curRow;
  4256. initialVertex.col = curCol;
  4257. openList->clear();
  4258. #ifdef _DEBUG
  4259. long insertErr =
  4260. #endif
  4261. openList->insert(initialVertex);
  4262. gosASSERT(insertErr == NO_ERR);
  4263. curMapNode->setFlag(MOVEFLAG_OPEN);
  4264. bool goalFound = false;
  4265. long bestRow = -1;
  4266. long bestCol = -1;
  4267. #ifdef DEBUG_PATH
  4268. topOpenNodes = 1;
  4269. numNodesVisited = 1;
  4270. #endif
  4271. while (!openList->isEmpty()) {
  4272. #ifdef DEBUG_MOVE_MAP
  4273. if (debugMoveMap) {
  4274. File* pathDebugFile = new File;
  4275. pathDebugFile->create("mm.dbg");
  4276. writeDebug(pathDebugFile);
  4277. pathDebugFile->close();
  4278. delete pathDebugFile;
  4279. pathDebugFile = NULL;
  4280. }
  4281. #endif
  4282. #ifdef DEBUG_PATH
  4283. if (topOpenNodes < openList->getNumItems())
  4284. topOpenNodes = openList->getNumItems();
  4285. #endif
  4286. //----------------------
  4287. // Grab the best node...
  4288. PQNode bestPQNode;
  4289. openList->remove(bestPQNode);
  4290. bestRow = bestPQNode.row;
  4291. bestCol = bestPQNode.col;
  4292. MoveMapNodePtr bestMapNode = &map[bestPQNode.id];
  4293. bestMapNode->clearFlag(MOVEFLAG_OPEN);
  4294. long bestNodeG = bestMapNode->g;
  4295. //----------------------------
  4296. // Now, close the best node...
  4297. bestMapNode->setFlag(MOVEFLAG_CLOSED);
  4298. //--------------------------
  4299. // Have we found the goal...
  4300. if (bestMapNode->flags & MOVEFLAG_GOAL) {
  4301. goalFound = true;
  4302. break;
  4303. }
  4304. for (long dir = 0; dir < 8; dir++) {
  4305. //------------------------------------------------------------
  4306. // First, make sure this is a legit direction to go. We do NOT
  4307. // want to clip corners, so we'll check that here...
  4308. bool isDiagonalWalk = IsDiagonalStep[dir];
  4309. if (isDiagonalWalk) {
  4310. // MINE CHECK should go in these adj tests...
  4311. bool adj1Open = false;
  4312. long adjCellIndex = map[bestPQNode.id].adjCells[StepAdjDir[dir]];
  4313. if (adjCellIndex > -1)
  4314. if ((map[adjCellIndex].flags & MOVEFLAG_MOVER_HERE) == 0)
  4315. adj1Open = (map[adjCellIndex].cost < COST_BLOCKED);
  4316. bool adj2Open = false;
  4317. adjCellIndex = map[bestPQNode.id].adjCells[StepAdjDir[dir + 1]];
  4318. if (adjCellIndex > -1)
  4319. if ((map[adjCellIndex].flags & MOVEFLAG_MOVER_HERE) == 0)
  4320. adj2Open = (map[adjCellIndex].cost < COST_BLOCKED);
  4321. if (!adj1Open && !adj2Open)
  4322. continue;
  4323. //if (!adjacentCellOpen(bestPQNode.id, StepAdjDir[dir]) && !adjacentCellOpen(bestPQNode.id, StepAdjDir[dir + 1]))
  4324. // continue;
  4325. }
  4326. //-------------------------------
  4327. // Now, process this direction...
  4328. long succCellIndex = bestMapNode->adjCells[dir];
  4329. //-----------------------------------------------------------------------------------
  4330. // If we're doing offMapTravel, make sure we aren't going back into an offMap cell...
  4331. if (map[succCellIndex].flags & MOVEFLAG_OFFMAP)
  4332. if (cannotEnterOffMap)
  4333. if ((map[bestPQNode.id].flags & MOVEFLAG_OFFMAP) == 0)
  4334. continue;
  4335. //--------------------------------
  4336. // If it's on the map, check it...
  4337. if (succCellIndex > -1) {
  4338. if (!inBounds(mapRowTable[succCellIndex], mapColTable[succCellIndex]))
  4339. continue;
  4340. MoveMapNodePtr succMapNode = &map[succCellIndex];
  4341. if (succMapNode->cost < COST_BLOCKED) {
  4342. if (succMapNode->hPrime == HPRIME_NOT_CALCED)
  4343. succMapNode->hPrime = calcHPrime(mapRowTable[succCellIndex/*bestPQNode.id*/], mapColTable[succCellIndex/*bestPQNode.id*/]);
  4344. if (succMapNode->hPrime < MaxHPrime) {
  4345. #ifdef DEBUG_PATH
  4346. numNodesVisited++;
  4347. #endif
  4348. //--------------------------------------------------
  4349. // How can we get back to BESTNODE from SUCCESSOR...
  4350. char dirToParent = reverseShift[dir];
  4351. //----------------------------------------------------
  4352. // What's our cost to go from START to this SUCCESSOR?
  4353. long cost = succMapNode->cost;
  4354. //------------------------------------
  4355. // Diagonal movement is more costly...
  4356. gosASSERT(cost > 0);
  4357. if (isDiagonalWalk)
  4358. cost += (cost / 2);
  4359. gosASSERT(cost > 0);
  4360. long succNodeG = bestNodeG + cost;
  4361. if (succMapNode->flags & MOVEFLAG_OPEN) {
  4362. //----------------------------------------------
  4363. // This node is already in the OPEN queue to be
  4364. // be processed. Let's check if we have a better
  4365. // path thru this route...
  4366. if (succNodeG < succMapNode->g) {
  4367. //----------------------------
  4368. // This new path is cheaper...
  4369. succMapNode->parent = dirToParent;
  4370. succMapNode->g = succNodeG;
  4371. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  4372. long openIndex = openList->find(succCellIndex);
  4373. if (!openIndex) {
  4374. char s[128];
  4375. sprintf(s, "MoveMap.calcPath: Cannot find movemap node [%d, %d] for change\n", succCellIndex, dir);
  4376. #ifdef USE_OBJECTS
  4377. DebugOpenList(s);
  4378. #endif
  4379. gosASSERT(openIndex != NULL);
  4380. }
  4381. else
  4382. openList->change(openIndex, succMapNode->fPrime);
  4383. }
  4384. }
  4385. else if (succMapNode->flags & MOVEFLAG_CLOSED) {
  4386. //-------------------------------------------------
  4387. // This path may be better than this node's current
  4388. // path. If so, we may have to propogate thru...
  4389. if (succNodeG < succMapNode->g) {
  4390. //----------------------------
  4391. // This new path is cheaper...
  4392. succMapNode->parent = dirToParent;
  4393. //------------------------------------------
  4394. // Now, we have to propogate the new cost...
  4395. propogateCost(succCellIndex, cost, bestNodeG);
  4396. }
  4397. }
  4398. else {
  4399. //-------------------------------------------------
  4400. // This node is neither OPEN nor CLOSED, so toss it
  4401. // into the OPEN list...
  4402. succMapNode->parent = dirToParent;
  4403. succMapNode->g = succNodeG;
  4404. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  4405. PQNode succPQNode;
  4406. succPQNode.key = succMapNode->fPrime;
  4407. succPQNode.id = succCellIndex;
  4408. succPQNode.row = mapRowTable[succCellIndex];
  4409. succPQNode.col = mapColTable[succCellIndex];
  4410. #ifdef _DEBUG
  4411. long insertErr =
  4412. #endif
  4413. openList->insert(succPQNode);
  4414. gosASSERT(insertErr == NO_ERR);
  4415. succMapNode->setFlag(MOVEFLAG_OPEN);
  4416. }
  4417. }
  4418. }
  4419. }
  4420. }
  4421. }
  4422. #ifdef DEBUG_MOVE_MAP
  4423. if (debugMoveMap) {
  4424. File* pathDebugFile = new File;
  4425. pathDebugFile->create("mm.dbg");
  4426. writeDebug(pathDebugFile);
  4427. pathDebugFile->close();
  4428. delete pathDebugFile;
  4429. pathDebugFile = NULL;
  4430. }
  4431. #endif
  4432. if (goalFound) {
  4433. //-------------------------------------------
  4434. // First, let's count how long the path is...
  4435. long curRow = goalCell[0] = (long)bestRow;
  4436. long curCol = goalCell[1] = (long)bestCol;
  4437. long numCells = 0;
  4438. while ((curRow != startR) || (curCol != startC)) {
  4439. numCells += 1;
  4440. long cellOffsetIndex = (map[mapRowStartTable[curRow] + curCol].parent << 1);
  4441. //if ((cellOffsetIndex < 0) || (cellOffsetIndex > 14))
  4442. // OutputDebugString("PathFinder: whoops\n");
  4443. curRow += cellShift[cellOffsetIndex++];
  4444. curCol += cellShift[cellOffsetIndex];
  4445. }
  4446. //---------------------------------------------------------------
  4447. // If our goal is a door, the path will be one step longer, since
  4448. // we need to walk "thru" the door...
  4449. if (doorDirection != -1)
  4450. numCells++;
  4451. #ifdef _DEBUG
  4452. if (numCells > MAX_STEPS_PER_MOVEPATH) {
  4453. File* pathDebugFile = new File;
  4454. pathDebugFile->create("longpath.dbg");
  4455. char s[512];
  4456. sprintf(s, "Path Too Long: %d steps (Max = %d) (please save longpath.dbg file:)", numCells, MAX_STEPS_PER_MOVEPATH);
  4457. pathDebugFile->writeString(s);
  4458. pathDebugFile->writeString("\n\n");
  4459. writeDebug(pathDebugFile);
  4460. pathDebugFile->close();
  4461. delete pathDebugFile;
  4462. pathDebugFile = NULL;
  4463. gosASSERT(numCells <= MAX_STEPS_PER_MOVEPATH);
  4464. }
  4465. #endif
  4466. //-----------------------------
  4467. // Now, let's build the path...
  4468. //-------------------------------
  4469. // Grab the path and return it...
  4470. path->init();
  4471. if (numCells) {
  4472. #ifdef _DEBUG
  4473. long maxSteps =
  4474. #endif
  4475. path->init(numCells);
  4476. #ifdef _DEBUG
  4477. if (maxSteps > -1) {
  4478. File* pathDebugFile = new File;
  4479. pathDebugFile->create("longpath.dbg");
  4480. char s[512];
  4481. sprintf(s, "New Longest Path: %d steps (Max = %d)\n\n", numCells, MAX_STEPS_PER_MOVEPATH);
  4482. pathDebugFile->writeString(s);
  4483. writeDebug(pathDebugFile);
  4484. pathDebugFile->close();
  4485. delete pathDebugFile;
  4486. pathDebugFile = NULL;
  4487. }
  4488. #endif
  4489. path->target = target;
  4490. path->cost = map[mapRowStartTable[bestRow] + bestCol].g;
  4491. curRow = (long)bestRow;
  4492. curCol = (long)bestCol;
  4493. long curCell = numCells;
  4494. if (doorDirection == -1) {
  4495. if (goalWorldPos) {
  4496. goalWorldPos->x = (float)(ULc + bestCol) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4497. goalWorldPos->y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + bestRow) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4498. goalWorldPos->z = (float)0; // How do we get the elevation for this point? Do we care?
  4499. path->goal = *goalWorldPos;
  4500. }
  4501. else
  4502. path->goal = goal;
  4503. }
  4504. else {
  4505. //--------------------------------------------------
  4506. // It's a door, so it's the last on the path list...
  4507. curCell--;
  4508. path->setDirection(curCell, /*reverseShift[*/doorDirection * 2/*]*/);
  4509. long doorR = bestRow + adjTile[doorDirection][0];
  4510. long doorC = bestCol + adjTile[doorDirection][1];
  4511. goalCell[0] = ULr + doorR;
  4512. goalCell[1] = ULc + doorC;
  4513. Stuff::Vector3D stepDest;
  4514. stepDest.x = (float)(ULc + doorC) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4515. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + doorR) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4516. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  4517. path->setDestination(curCell, stepDest);
  4518. path->setDistanceToGoal(curCell, 0.0);
  4519. path->setCell(curCell, goalCell[0], goalCell[1]);
  4520. path->goal = stepDest;
  4521. if (goalWorldPos) {
  4522. //--------------------------------------------------------------------
  4523. // We didn't know the exact goal world pos coming into this, since are
  4524. // goal was an area door....
  4525. *goalWorldPos = stepDest;
  4526. }
  4527. }
  4528. //---------------------------------------------------------
  4529. // We need to set this table up our first time thru here...
  4530. static bool setTable = false;
  4531. if (!setTable) {
  4532. float cellLength = Terrain::worldUnitsPerCell * metersPerWorldUnit;
  4533. for (long i = 0; i < NUM_CELL_OFFSETS; i++) {
  4534. float distance = agsqrt( cellShift[i * 2], cellShift[i * 2 + 1] ) * cellLength;
  4535. cellShiftDistance[i] = distance;
  4536. }
  4537. setTable = true;
  4538. }
  4539. while ((curRow != startR) || (curCol != startC)) {
  4540. curCell--;
  4541. long parent = reverseShift[map[mapRowStartTable[curRow] + curCol].parent];
  4542. if (parent > 7)
  4543. path->setDirection(curCell, parent);
  4544. else
  4545. path->setDirection(curCell, parent);
  4546. Stuff::Vector3D stepDest;
  4547. long cell[2];
  4548. cell[0] = ULr + curRow;
  4549. cell[1] = ULc + curCol;
  4550. stepDest.x = (float)(cell[1]) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4551. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(cell[0]) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4552. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  4553. if (curCell == (numCells - 1))
  4554. path->setDistanceToGoal(curCell, 0.0);
  4555. else {
  4556. long tempDir = path->getDirection(curCell + 1);
  4557. float tempDist = path->getDistanceToGoal(curCell + 1);
  4558. path->setDistanceToGoal(curCell, cellShiftDistance[tempDir] + tempDist);
  4559. }
  4560. path->setDestination(curCell, stepDest);
  4561. path->setCell(curCell, cell[0], cell[1]);
  4562. path->stepList[curCell].area = GlobalMoveMap[moveLevel]->calcArea(cell[0], cell[1]);
  4563. map[curRow * maxWidth + curCol].setFlag(MOVEFLAG_STEP);
  4564. long cellOffsetIndex = map[mapRowStartTable[curRow] + curCol].parent << 1;
  4565. curRow += cellShift[cellOffsetIndex++];
  4566. curCol += cellShift[cellOffsetIndex];
  4567. //calcAdjNode(curRow, curCol, map[curRow * maxCellWidth + curCol].parent);
  4568. }
  4569. if (thruAreas[1] != -1)
  4570. if (thruAreas[1] != path->stepList[path->numSteps - 1].area) {
  4571. for (long i = 0; i < path->numSteps; i++) {
  4572. if (path->stepList[i].area == thruAreas[1]) {
  4573. path->numStepsWhenNotPaused = path->numSteps = i + 1;
  4574. goalCell[0] = path->stepList[i].cell[0];
  4575. goalCell[1] = path->stepList[i].cell[1];
  4576. land->cellToWorld(goalCell[0], goalCell[1], *goalWorldPos);
  4577. break;
  4578. }
  4579. }
  4580. }
  4581. #ifdef BLOCKED_PATH_TEST
  4582. //---------------------------------------------------------------------
  4583. // Let's test every path point to make sure it is not a blocked cell...
  4584. for (long i = 0; i < path->numSteps; i++) {
  4585. long tile[2], cell[2];
  4586. GameMap->worldToMapPos(path->stepList[i].destination, tile[0], tile[1], cell[0], cell[1]);
  4587. bool cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  4588. if (!cellPassable)
  4589. cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  4590. Assert(cellPassable || (i < 3), 0, " Bad Path Point ");
  4591. }
  4592. #endif
  4593. }
  4594. #ifdef TIME_PATH
  4595. QueryPerformanceCounter(calcStop);
  4596. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  4597. if (debugger && (scenarioTime > 0.0)) {
  4598. char s[50];
  4599. sprintf(s, "path calc: %.4f\n", calcTime);
  4600. OutputDebugString(s);
  4601. debugger->print(s);
  4602. }
  4603. #endif
  4604. //------------------------------------------------------------------------------------
  4605. // If we're starting on the goal cell, set the cost to 1 as an indicator that there is
  4606. // a path--it's just that we were already there!
  4607. if (numCells == 0)
  4608. path->cost = 1;
  4609. return(path->numSteps);
  4610. }
  4611. #ifdef TIME_PATH
  4612. QueryPerformanceCounter(calcStop);
  4613. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  4614. if (debugger && (scenarioTime > 0.0)) {
  4615. char s[50];
  4616. sprintf(s, "path calc: %.4f", calcTime);
  4617. debugger->print(s);
  4618. }
  4619. #endif
  4620. return(0);
  4621. }
  4622. //---------------------------------------------------------------------------
  4623. long MoveMap::calcPathJUMP (MovePathPtr path, Stuff::Vector3D* goalWorldPos, long* goalCell) {
  4624. #ifdef TIME_PATH
  4625. L_INTEGER calcStart, calcStop;
  4626. QueryPerformanceCounter(calcStart);
  4627. #endif
  4628. //------------------------------------------------------------------
  4629. // Clear the start cell, in case we're starting in a blocked cell...
  4630. //map[startR * maxCellWidth + startC].cost = clearCost;
  4631. //-------------------------------------------------------------------
  4632. // If the start tile is blocked, let's clear it so we may move off of
  4633. // it, at least. Change the blocked tiles to just barely passable...
  4634. /* long startULr = startR - 1;
  4635. long startULc = startC - 1;
  4636. for (long cellR = 0; cellR < MAPCELL_DIM; cellR++)
  4637. for (long cellC = 0; cellC < MAPCELL_DIM; cellC++) {
  4638. long curCellR = startULr + cellR;
  4639. long curCellC = startULc + cellC;
  4640. long mapIndex = curCellR * maxCellWidth + curCellC;
  4641. if ((curCellR >= 0) && (curCellR < cellHeight) && (curCellC >= 0) && (curCellC < cellWidth))
  4642. if (map[mapIndex].cost >= COST_BLOCKED)
  4643. map[mapIndex].cost = COST_BLOCKED - 1;
  4644. }
  4645. */
  4646. if ((goalR < 0) || (goalR >= height) || (goalC < 0) || (goalC >= width)) {
  4647. Stuff::Vector3D p;
  4648. p.x = (float)(goalC) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4649. p.y = (Terrain::worldUnitsMapSide / 2) - ((float)(goalR) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4650. p.z = (float)0; // How do we get the elevation for this point? Do we care?
  4651. char msg[200];
  4652. sprintf(msg, " Bad Move Goal: %d [%d(%d), %d(%d)], (%.2f, %.2f, %.2f)", DebugMovePathType, goalR, height, goalC, width, p.x, p.y, p.z);
  4653. gosASSERT((goalR >= 0) && (goalR < height) && (goalC >= 0) && (goalC < width));
  4654. }
  4655. //------------------------------------------------------------------
  4656. // Let's use their hPrime as a barrier for cutting off the search...
  4657. MaxHPrime = calcHPrime(startR, startC) * 2.5;
  4658. if (MaxHPrime < 500)
  4659. MaxHPrime = 500;
  4660. //-----------------------------------------------
  4661. // If we haven't already, create the OPEN list...
  4662. if (!openList) {
  4663. openList = new PriorityQueue;
  4664. gosASSERT(openList != NULL);
  4665. openList->init(5000);
  4666. }
  4667. long curCol = startC;
  4668. long curRow = startR;
  4669. MoveMapNodePtr curMapNode = &map[curRow * maxWidth + curCol];
  4670. curMapNode->g = 0;
  4671. if (!ZeroHPrime)
  4672. curMapNode->hPrime = calcHPrime(curRow, curCol);
  4673. curMapNode->fPrime = curMapNode->hPrime;
  4674. //-----------------------------------------------
  4675. // Put the START vertex on the empty OPEN list...
  4676. PQNode initialVertex;
  4677. initialVertex.key = curMapNode->fPrime;
  4678. initialVertex.id = curRow * MAX_MAPWIDTH + curCol;
  4679. initialVertex.row = curRow;
  4680. initialVertex.col = curCol;
  4681. openList->clear();
  4682. #ifdef _DEBUG
  4683. long insertErr =
  4684. #endif
  4685. openList->insert(initialVertex);
  4686. gosASSERT(insertErr == NO_ERR);
  4687. curMapNode->setFlag(MOVEFLAG_OPEN);
  4688. bool goalFound = false;
  4689. long bestRow = -1;
  4690. long bestCol = -1;
  4691. #ifdef DEBUG_PATH
  4692. topOpenNodes = 1;
  4693. numNodesVisited = 1;
  4694. #endif
  4695. while (!openList->isEmpty()) {
  4696. #ifdef DEBUG_MOVE_MAP
  4697. if (debugMoveMap) {
  4698. File* pathDebugFile = new File;
  4699. pathDebugFile->create("mm.dbg");
  4700. writeDebug(pathDebugFile);
  4701. pathDebugFile->close();
  4702. delete pathDebugFile;
  4703. pathDebugFile = NULL;
  4704. }
  4705. #endif
  4706. #ifdef DEBUG_PATH
  4707. if (topOpenNodes < openList->getNumItems())
  4708. topOpenNodes = openList->getNumItems();
  4709. #endif
  4710. //----------------------
  4711. // Grab the best node...
  4712. PQNode bestPQNode;
  4713. openList->remove(bestPQNode);
  4714. bestRow = bestPQNode.row;
  4715. bestCol = bestPQNode.col;
  4716. MoveMapNodePtr bestMapNode = &map[bestRow * maxWidth + bestCol];
  4717. bestMapNode->clearFlag(MOVEFLAG_OPEN);
  4718. long bestNodeG = bestMapNode->g;
  4719. //----------------------------
  4720. // Now, close the best node...
  4721. bestMapNode->setFlag(MOVEFLAG_CLOSED);
  4722. //--------------------------
  4723. // Have we found the goal...
  4724. if (bestMapNode->flags & MOVEFLAG_GOAL) {
  4725. goalFound = true;
  4726. break;
  4727. }
  4728. long cellOffsetIndex = 0;
  4729. for (long dir = 0; dir < numOffsets; dir++) {
  4730. //------------------------------------------------------------
  4731. // First, make sure this is a legit direction to go. We do NOT
  4732. // want to clip corners, so we'll check that here...
  4733. bool isDiagonalWalk = IsDiagonalStep[dir];
  4734. if (isDiagonalWalk) {
  4735. if (!adjacentCellOpenJUMP(bestRow, bestCol, StepAdjDir[dir]) && !adjacentCellOpenJUMP(bestRow, bestCol, StepAdjDir[dir + 1])) {
  4736. cellOffsetIndex += 2;
  4737. continue;
  4738. }
  4739. }
  4740. //------------------------------------------------------------
  4741. // Now, process this direction. First, calc the cell to check,
  4742. // offset from the current cell...
  4743. long succRow = bestRow + cellShift[cellOffsetIndex++];
  4744. long succCol = bestCol + cellShift[cellOffsetIndex++];
  4745. //--------------------------------
  4746. // If it's on the map, check it...
  4747. if (inMapBounds(succRow, succCol, height, width)) {
  4748. MoveMapNodePtr succMapNode = &map[succRow * maxWidth + succCol];
  4749. if (succMapNode->cost < COST_BLOCKED) {
  4750. if (succMapNode->hPrime == HPRIME_NOT_CALCED)
  4751. succMapNode->hPrime = calcHPrime(succRow, succCol);
  4752. if (succMapNode->hPrime < MaxHPrime) {
  4753. #ifdef DEBUG_PATH
  4754. numNodesVisited++;
  4755. #endif
  4756. //--------------------------------------------------
  4757. // How can we get back to BESTNODE from SUCCESSOR...
  4758. char dirToParent = reverseShift[dir];
  4759. //----------------------------------------------------
  4760. // What's our cost to go from START to this SUCCESSOR?
  4761. bool jumping = false;
  4762. long cost = succMapNode->cost;
  4763. //------------------------------------
  4764. // Diagonal movement is more costly...
  4765. gosASSERT(cost > 0);
  4766. if (dir > 7) {
  4767. jumping = true;
  4768. if (JumpOnBlocked)
  4769. cost = jumpCost;
  4770. else
  4771. cost += jumpCost;
  4772. }
  4773. else {
  4774. if (isDiagonalWalk)
  4775. cost += (cost / 2);
  4776. }
  4777. gosASSERT(cost > 0);
  4778. long succNodeG = bestNodeG + cost;
  4779. if (succMapNode->flags & MOVEFLAG_OPEN) {
  4780. //----------------------------------------------
  4781. // This node is already in the OPEN queue to be
  4782. // be processed. Let's check if we have a better
  4783. // path thru this route...
  4784. if (succNodeG < succMapNode->g) {
  4785. //----------------------------
  4786. // This new path is cheaper...
  4787. succMapNode->parent = dirToParent;
  4788. succMapNode->g = succNodeG;
  4789. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  4790. long openIndex = openList->find(succRow * MAX_MAPWIDTH + succCol);
  4791. if (!openIndex) {
  4792. char s[128];
  4793. sprintf(s, "MoveMap.calcPath: Cannot find movemap node [%d, %d, %d, %d] for change\n", succRow, succCol, succRow * MAX_MAPWIDTH + succCol, dir);
  4794. #ifdef USE_OBJECTS
  4795. DebugOpenList(s);
  4796. #endif
  4797. gosASSERT(openIndex != NULL);
  4798. }
  4799. else
  4800. openList->change(openIndex, succMapNode->fPrime);
  4801. }
  4802. }
  4803. else if (succMapNode->flags & MOVEFLAG_CLOSED) {
  4804. //-------------------------------------------------
  4805. // This path may be better than this node's current
  4806. // path. If so, we may have to propogate thru...
  4807. if (succNodeG < succMapNode->g) {
  4808. //----------------------------
  4809. // This new path is cheaper...
  4810. succMapNode->parent = dirToParent;
  4811. //------------------------------------------
  4812. // Now, we have to propogate the new cost...
  4813. propogateCostJUMP(succRow, succCol, cost, bestNodeG);
  4814. }
  4815. }
  4816. else {
  4817. //-------------------------------------------------
  4818. // This node is neither OPEN nor CLOSED, so toss it
  4819. // into the OPEN list...
  4820. succMapNode->parent = dirToParent;
  4821. succMapNode->g = succNodeG;
  4822. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  4823. PQNode succPQNode;
  4824. succPQNode.key = succMapNode->fPrime;
  4825. succPQNode.id = succRow * MAX_MAPWIDTH + succCol;
  4826. succPQNode.row = succRow;
  4827. succPQNode.col = succCol;
  4828. #ifdef _DEBUG
  4829. long insertErr =
  4830. #endif
  4831. openList->insert(succPQNode);
  4832. gosASSERT(insertErr == NO_ERR);
  4833. succMapNode->setFlag(MOVEFLAG_OPEN);
  4834. }
  4835. }
  4836. }
  4837. }
  4838. }
  4839. }
  4840. #ifdef DEBUG_MOVE_MAP
  4841. if (debugMoveMap) {
  4842. File* pathDebugFile = new File;
  4843. pathDebugFile->create("mm.dbg");
  4844. writeDebug(pathDebugFile);
  4845. pathDebugFile->close();
  4846. delete pathDebugFile;
  4847. pathDebugFile = NULL;
  4848. }
  4849. #endif
  4850. #if 0 //REdo when bridges are done
  4851. if (ClearBridgeTiles) {
  4852. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  4853. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  4854. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  4855. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  4856. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  4857. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  4858. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  4859. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  4860. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  4861. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  4862. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  4863. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  4864. }
  4865. #endif
  4866. if (goalFound) {
  4867. //-------------------------------------------
  4868. // First, let's count how long the path is...
  4869. long curRow = goalCell[0] = (long)bestRow;
  4870. long curCol = goalCell[1] = (long)bestCol;
  4871. long numCells = 0;
  4872. while ((curRow != startR) || (curCol != startC)) {
  4873. numCells += 1;
  4874. long cellOffsetIndex = map[curRow * maxWidth + curCol].parent * 2;
  4875. //if ((cellOffsetIndex < 0) || (cellOffsetIndex > 14))
  4876. // OutputDebugString("PathFinder: whoops\n");
  4877. curRow += cellShift[cellOffsetIndex++];
  4878. curCol += cellShift[cellOffsetIndex];
  4879. }
  4880. //---------------------------------------------------------------
  4881. // If our goal is a door, the path will be one step longer, since
  4882. // we need to walk "thru" the door...
  4883. if (doorDirection != -1)
  4884. numCells++;
  4885. #ifdef _DEBUG
  4886. if (numCells > MAX_STEPS_PER_MOVEPATH) {
  4887. File* pathDebugFile = new File;
  4888. pathDebugFile->create("longpath.dbg");
  4889. char s[512];
  4890. sprintf(s, "Path Too Long: %d steps (Max = %d) (please save longpath.dbg file:)", numCells, MAX_STEPS_PER_MOVEPATH);
  4891. pathDebugFile->writeString(s);
  4892. pathDebugFile->writeString("\n\n");
  4893. writeDebug(pathDebugFile);
  4894. pathDebugFile->close();
  4895. delete pathDebugFile;
  4896. pathDebugFile = NULL;
  4897. gosASSERT(numCells <= MAX_STEPS_PER_MOVEPATH);
  4898. }
  4899. #endif
  4900. //-----------------------------
  4901. // Now, let's build the path...
  4902. //-------------------------------
  4903. // Grab the path and return it...
  4904. path->init();
  4905. if (numCells) {
  4906. #ifdef _DEBUG
  4907. long maxSteps =
  4908. #endif
  4909. path->init(numCells);
  4910. #ifdef _DEBUG
  4911. if (maxSteps > -1) {
  4912. File* pathDebugFile = new File;
  4913. pathDebugFile->create("longpath.dbg");
  4914. char s[512];
  4915. sprintf(s, "New Longest Path: %d steps (Max = %d)\n\n", numCells, MAX_STEPS_PER_MOVEPATH);
  4916. pathDebugFile->writeString(s);
  4917. writeDebug(pathDebugFile);
  4918. pathDebugFile->close();
  4919. delete pathDebugFile;
  4920. pathDebugFile = NULL;
  4921. }
  4922. #endif
  4923. path->target = target;
  4924. path->cost = map[bestRow * maxWidth + bestCol].g;
  4925. curRow = (long)bestRow;
  4926. curCol = (long)bestCol;
  4927. long curCell = numCells;
  4928. if (doorDirection == -1) {
  4929. if (goalWorldPos) {
  4930. goalWorldPos->x = (float)(ULc + bestCol) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4931. goalWorldPos->y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + bestRow) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4932. goalWorldPos->z = (float)0; // How do we get the elevation for this point? Do we care?
  4933. path->goal = *goalWorldPos;
  4934. }
  4935. else
  4936. path->goal = goal;
  4937. }
  4938. else {
  4939. //--------------------------------------------------
  4940. // It's a door, so it's the last on the path list...
  4941. curCell--;
  4942. path->setDirection(curCell, /*reverseShift[*/doorDirection * 2/*]*/);
  4943. long doorR = bestRow + adjTile[doorDirection][0];
  4944. long doorC = bestCol + adjTile[doorDirection][1];
  4945. goalCell[0] = ULr + doorR;
  4946. goalCell[1] = ULc + doorC;
  4947. Stuff::Vector3D stepDest;
  4948. stepDest.x = (float)(ULc + doorC) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4949. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + doorR) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4950. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  4951. path->setDestination(curCell, stepDest);
  4952. path->setDistanceToGoal(curCell, 0.0);
  4953. path->setCell(curCell, goalCell[0], goalCell[1]);
  4954. path->goal = stepDest;
  4955. if (goalWorldPos) {
  4956. //--------------------------------------------------------------------
  4957. // We didn't know the exact goal world pos coming into this, since are
  4958. // goal was an area door....
  4959. *goalWorldPos = stepDest;
  4960. }
  4961. }
  4962. //---------------------------------------------------------
  4963. // We need to set this table up our first time thru here...
  4964. static bool setTable = false;
  4965. if (!setTable) {
  4966. float cellLength = Terrain::worldUnitsPerCell * metersPerWorldUnit;
  4967. for (long i = 0; i < NUM_CELL_OFFSETS; i++) {
  4968. float distance = agsqrt( cellShift[i * 2], cellShift[i * 2 + 1] ) * cellLength;
  4969. cellShiftDistance[i] = distance;
  4970. }
  4971. setTable = true;
  4972. }
  4973. while ((curRow != startR) || (curCol != startC)) {
  4974. curCell--;
  4975. long parent = reverseShift[map[curRow * maxWidth + curCol].parent];
  4976. if (parent > 7)
  4977. path->setDirection(curCell, parent);
  4978. else
  4979. path->setDirection(curCell, parent);
  4980. Stuff::Vector3D stepDest;
  4981. long cell[2];
  4982. cell[0] = ULr + curRow;
  4983. cell[1] = ULc + curCol;
  4984. stepDest.x = (float)(cell[1]) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  4985. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(cell[0]) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  4986. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  4987. if (curCell == (numCells - 1))
  4988. path->setDistanceToGoal(curCell, 0.0);
  4989. else {
  4990. long tempDir = path->getDirection(curCell + 1);
  4991. float tempDist = path->getDistanceToGoal(curCell + 1);
  4992. path->setDistanceToGoal(curCell, cellShiftDistance[tempDir] + tempDist);
  4993. }
  4994. path->setDestination(curCell, stepDest);
  4995. path->setCell(curCell, cell[0], cell[1]);
  4996. map[curRow * maxWidth + curCol].setFlag(MOVEFLAG_STEP);
  4997. long cellOffsetIndex = map[curRow * maxWidth + curCol].parent << 1;
  4998. curRow += cellShift[cellOffsetIndex++];
  4999. curCol += cellShift[cellOffsetIndex];
  5000. //calcAdjNode(curRow, curCol, map[curRow * maxCellWidth + curCol].parent);
  5001. }
  5002. #ifdef BLOCKED_PATH_TEST
  5003. //---------------------------------------------------------------------
  5004. // Let's test every path point to make sure it is not a blocked cell...
  5005. for (long i = 0; i < path->numSteps; i++) {
  5006. long tile[2], cell[2];
  5007. GameMap->worldToMapPos(path->stepList[i].destination, tile[0], tile[1], cell[0], cell[1]);
  5008. bool cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  5009. if (!cellPassable)
  5010. cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  5011. Assert(cellPassable || (i < 3), 0, " Bad Path Point ");
  5012. }
  5013. #endif
  5014. }
  5015. #ifdef TIME_PATH
  5016. QueryPerformanceCounter(calcStop);
  5017. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  5018. if (debugger && (scenarioTime > 0.0)) {
  5019. char s[50];
  5020. sprintf(s, "path calc: %.4f\n", calcTime);
  5021. OutputDebugString(s);
  5022. debugger->print(s);
  5023. }
  5024. #endif
  5025. return(path->numSteps);
  5026. }
  5027. #ifdef TIME_PATH
  5028. QueryPerformanceCounter(calcStop);
  5029. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  5030. if (debugger && (scenarioTime > 0.0)) {
  5031. char s[50];
  5032. sprintf(s, "path calc: %.4f", calcTime);
  5033. debugger->print(s);
  5034. }
  5035. #endif
  5036. return(0);
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. long MoveMap::calcEscapePath (MovePathPtr path, Stuff::Vector3D* goalWorldPos, long* goalCell) {
  5040. #ifdef TIME_PATH
  5041. L_INTEGER calcStart, calcStop;
  5042. QueryPerformanceCounter(calcStart);
  5043. #endif
  5044. //------------------------------------------------------------------
  5045. // Clear the start cell, in case we're starting in a blocked cell...
  5046. //map[startR * maxCellWidth + startC].cost = clearCost;
  5047. //-------------------------------------------------------------------
  5048. // If the start tile is blocked, let's clear it so we may move off of
  5049. // it, at least. Change the blocked tiles to just barely passable...
  5050. /* long startULr = startR - 1;
  5051. long startULc = startC - 1;
  5052. for (long cellR = 0; cellR < MAPCELL_DIM; cellR++)
  5053. for (long cellC = 0; cellC < MAPCELL_DIM; cellC++) {
  5054. long curCellR = startULr + cellR;
  5055. long curCellC = startULc + cellC;
  5056. long mapIndex = curCellR * maxCellWidth + curCellC;
  5057. if ((curCellR >= 0) && (curCellR < cellHeight) && (curCellC >= 0) && (curCellC < cellWidth))
  5058. if (map[mapIndex].cost >= COST_BLOCKED)
  5059. map[mapIndex].cost = COST_BLOCKED - 1;
  5060. }
  5061. */
  5062. /*
  5063. if ((goalR < 0) || (goalR >= cellHeight) || (goalC < 0) || (goalC >= cellWidth)) {
  5064. Stuff::Vector3D p;
  5065. p.x = (float)(goalC) * MetersPerCell + MetersPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  5066. p.y = (Terrain::worldUnitsMapSide / 2) - ((float)(goalR) * MetersPerCell) - MetersPerCell / 2;
  5067. p.z = (float)0; // How do we get the elevation for this point? Do we care?
  5068. char msg[200];
  5069. sprintf(msg, " Bad Move Goal: %d [%d(%d), %d(%d)], (%.2f, %.2f, %.2f)", DebugMovePathType, goalR, cellHeight, goalC, cellWidth, p.x, p.y, p.z);
  5070. Fatal(0, msg);
  5071. }
  5072. */
  5073. //------------------------------------------------------------------
  5074. // Let's use their hPrime as a barrier for cutting off the search...
  5075. MaxHPrime = 500; //float2short(calcHPrime(startR, startC) * 2.5);
  5076. if (MaxHPrime < 500)
  5077. MaxHPrime = 500;
  5078. //-----------------------------------------------
  5079. // If we haven't already, create the OPEN list...
  5080. if (!openList) {
  5081. openList = new PriorityQueue;
  5082. gosASSERT(openList != NULL);
  5083. openList->init(5000);
  5084. }
  5085. long curCol = startC;
  5086. long curRow = startR;
  5087. MoveMapNodePtr curMapNode = &map[curRow * maxWidth + curCol];
  5088. curMapNode->g = 0;
  5089. curMapNode->hPrime = 10; //calcHPrime(curRow, curCol);
  5090. curMapNode->fPrime = curMapNode->hPrime;
  5091. //-----------------------------------------------
  5092. // Put the START vertex on the empty OPEN list...
  5093. PQNode initialVertex;
  5094. initialVertex.key = curMapNode->fPrime;
  5095. initialVertex.id = curRow * MAX_MAPWIDTH + curCol;
  5096. initialVertex.row = curRow;
  5097. initialVertex.col = curCol;
  5098. openList->clear();
  5099. #ifdef _DEBUG
  5100. long insertErr =
  5101. #endif
  5102. openList->insert(initialVertex);
  5103. gosASSERT(insertErr == NO_ERR);
  5104. curMapNode->setFlag(MOVEFLAG_OPEN);
  5105. bool goalFound = false;
  5106. long bestRow = -1;
  5107. long bestCol = -1;
  5108. #ifdef DEBUG_PATH
  5109. topOpenNodes = 1;
  5110. numNodesVisited = 1;
  5111. #endif
  5112. while (!openList->isEmpty()) {
  5113. #ifdef DEBUG_MOVE_MAP
  5114. if (debugMoveMap) {
  5115. File* pathDebugFile = new File;
  5116. pathDebugFile->create("mm.dbg");
  5117. writeDebug(pathDebugFile);
  5118. pathDebugFile->close();
  5119. delete pathDebugFile;
  5120. pathDebugFile = NULL;
  5121. }
  5122. #endif
  5123. #ifdef DEBUG_PATH
  5124. if (topOpenNodes < openList->getNumItems())
  5125. topOpenNodes = openList->getNumItems();
  5126. #endif
  5127. //----------------------
  5128. // Grab the best node...
  5129. PQNode bestPQNode;
  5130. openList->remove(bestPQNode);
  5131. bestRow = bestPQNode.row;
  5132. bestCol = bestPQNode.col;
  5133. MoveMapNodePtr bestMapNode = &map[bestRow * maxWidth + bestCol];
  5134. bestMapNode->clearFlag(MOVEFLAG_OPEN);
  5135. long bestNodeG = bestMapNode->g;
  5136. //----------------------------
  5137. // Now, close the best node...
  5138. bestMapNode->setFlag(MOVEFLAG_CLOSED);
  5139. //--------------------------
  5140. // Have we found the goal...
  5141. if (bestMapNode->flags & MOVEFLAG_GOAL) {
  5142. goalFound = true;
  5143. break;
  5144. }
  5145. long cellOffsetIndex = 0;
  5146. for (long dir = 0; dir < numOffsets; dir++) {
  5147. //------------------------------------------------------------
  5148. // First, make sure this is a legit direction to go. We do NOT
  5149. // want to clip corners, so we'll check that here...
  5150. bool isDiagonalWalk = IsDiagonalStep[dir];
  5151. if (isDiagonalWalk) {
  5152. if (!adjacentCellOpenJUMP(bestRow, bestCol, StepAdjDir[dir]) && !adjacentCellOpenJUMP(bestRow, bestCol, StepAdjDir[dir + 1])) {
  5153. cellOffsetIndex += 2;
  5154. continue;
  5155. }
  5156. }
  5157. //------------------------------------------------------------
  5158. // Now, process this direction. First, calc the cell to check,
  5159. // offset from the current cell...
  5160. long succRow = bestRow + cellShift[cellOffsetIndex++];
  5161. long succCol = bestCol + cellShift[cellOffsetIndex++];
  5162. //--------------------------------
  5163. // If it's on the map, check it...
  5164. if (inMapBounds(succRow, succCol, height, width)) {
  5165. MoveMapNodePtr succMapNode = &map[succRow * maxWidth + succCol];
  5166. if (succMapNode->cost < COST_BLOCKED) {
  5167. if (succMapNode->hPrime == HPRIME_NOT_CALCED)
  5168. succMapNode->hPrime = 10; //calcHPrime(succRow, succCol);
  5169. if (succMapNode->hPrime < MaxHPrime) {
  5170. #ifdef DEBUG_PATH
  5171. numNodesVisited++;
  5172. #endif
  5173. //--------------------------------------------------
  5174. // How can we get back to BESTNODE from SUCCESSOR...
  5175. char dirToParent = reverseShift[dir];
  5176. //----------------------------------------------------
  5177. // What's our cost to go from START to this SUCCESSOR?
  5178. bool jumping = false;
  5179. long cost = succMapNode->cost;
  5180. //------------------------------------
  5181. // Diagonal movement is more costly...
  5182. if (dir > 7) {
  5183. jumping = true;
  5184. if (JumpOnBlocked)
  5185. cost = jumpCost;
  5186. else
  5187. cost += jumpCost;
  5188. }
  5189. else {
  5190. if (isDiagonalWalk)
  5191. cost += (cost / 2);
  5192. }
  5193. gosASSERT(cost > 0);
  5194. long succNodeG = bestNodeG + cost;
  5195. if (succMapNode->flags & MOVEFLAG_OPEN) {
  5196. //----------------------------------------------
  5197. // This node is already in the OPEN queue to be
  5198. // be processed. Let's check if we have a better
  5199. // path thru this route...
  5200. if (succNodeG < succMapNode->g) {
  5201. //----------------------------
  5202. // This new path is cheaper...
  5203. succMapNode->parent = dirToParent;
  5204. succMapNode->g = succNodeG;
  5205. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  5206. long openIndex = openList->find(succRow * MAX_MAPWIDTH + succCol);
  5207. if (!openIndex) {
  5208. char s[128];
  5209. sprintf(s, "MoveMap.calcEscapePath: Cannot find movemap node [%d, %d, %d, %d] for change\n", succRow, succCol, succRow * MAX_MAPWIDTH + succCol, dir);
  5210. #ifdef USE_OBJECTS
  5211. DebugOpenList(s);
  5212. #endif
  5213. gosASSERT(openIndex != NULL);
  5214. }
  5215. else
  5216. openList->change(openIndex, succMapNode->fPrime);
  5217. }
  5218. }
  5219. else if (succMapNode->flags & MOVEFLAG_CLOSED) {
  5220. //-------------------------------------------------
  5221. // This path may be better than this node's current
  5222. // path. If so, we may have to propogate thru...
  5223. if (succNodeG < succMapNode->g) {
  5224. //----------------------------
  5225. // This new path is cheaper...
  5226. succMapNode->parent = dirToParent;
  5227. //------------------------------------------
  5228. // Now, we have to propogate the new cost...
  5229. propogateCostJUMP(succRow, succCol, cost, bestNodeG);
  5230. }
  5231. }
  5232. else {
  5233. //-------------------------------------------------
  5234. // This node is neither OPEN nor CLOSED, so toss it
  5235. // into the OPEN list...
  5236. succMapNode->parent = dirToParent;
  5237. succMapNode->g = succNodeG;
  5238. succMapNode->fPrime = succNodeG + succMapNode->hPrime;
  5239. PQNode succPQNode;
  5240. succPQNode.key = succMapNode->fPrime;
  5241. succPQNode.id = succRow * MAX_MAPWIDTH + succCol;
  5242. succPQNode.row = succRow;
  5243. succPQNode.col = succCol;
  5244. #ifdef _DEBUG
  5245. long insertErr =
  5246. #endif
  5247. openList->insert(succPQNode);
  5248. gosASSERT(insertErr == NO_ERR);
  5249. succMapNode->setFlag(MOVEFLAG_OPEN);
  5250. }
  5251. }
  5252. }
  5253. }
  5254. }
  5255. }
  5256. #ifdef DEBUG_MOVE_MAP
  5257. if (debugMoveMap) {
  5258. File* pathDebugFile = new File;
  5259. pathDebugFile->create("mm.dbg");
  5260. writeDebug(pathDebugFile);
  5261. pathDebugFile->close();
  5262. delete pathDebugFile;
  5263. pathDebugFile = NULL;
  5264. }
  5265. #endif
  5266. #if 0 //Redo when bridges are done
  5267. if (ClearBridgeTiles) {
  5268. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  5269. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  5270. overlayWeightTable[OVERLAY_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  5271. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  5272. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  5273. overlayWeightTable[OVERLAY_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  5274. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  5275. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  5276. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_NS * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  5277. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 1] += COST_BLOCKED;
  5278. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 4] += COST_BLOCKED;
  5279. overlayWeightTable[OVERLAY_RAILROAD_WATER_BRIDGE_EW * MAPCELL_DIM * MAPCELL_DIM + 7] += COST_BLOCKED;
  5280. }
  5281. #endif
  5282. if (goalFound) {
  5283. //-------------------------------------------
  5284. // First, let's count how long the path is...
  5285. long curRow = goalCell[0] = (long)bestRow;
  5286. long curCol = goalCell[1] = (long)bestCol;
  5287. long numCells = 0;
  5288. while ((curRow != startR) || (curCol != startC)) {
  5289. numCells += 1;
  5290. long cellOffsetIndex = map[curRow * maxWidth + curCol].parent * 2;
  5291. //if ((cellOffsetIndex < 0) || (cellOffsetIndex > 14))
  5292. // OutputDebugString("PathFinder: whoops\n");
  5293. curRow += cellShift[cellOffsetIndex++];
  5294. curCol += cellShift[cellOffsetIndex];
  5295. }
  5296. //---------------------------------------------------------------
  5297. // If our goal is a door, the path will be one step longer, since
  5298. // we need to walk "thru" the door...
  5299. if (doorDirection != -1)
  5300. numCells++;
  5301. #ifdef _DEBUG
  5302. if (numCells > MAX_STEPS_PER_MOVEPATH) {
  5303. File* pathDebugFile = new File;
  5304. pathDebugFile->create("longpath.dbg");
  5305. char s[512];
  5306. sprintf(s, "Path Too Long: %d steps (Max = %d) (please save longpath.dbg file:)", numCells, MAX_STEPS_PER_MOVEPATH);
  5307. pathDebugFile->writeString(s);
  5308. pathDebugFile->writeString("\n\n");
  5309. writeDebug(pathDebugFile);
  5310. pathDebugFile->close();
  5311. delete pathDebugFile;
  5312. pathDebugFile = NULL;
  5313. gosASSERT(numCells <= MAX_STEPS_PER_MOVEPATH);
  5314. }
  5315. #endif
  5316. //-----------------------------
  5317. // Now, let's build the path...
  5318. //-------------------------------
  5319. // Grab the path and return it...
  5320. path->init();
  5321. if (numCells) {
  5322. #ifdef _DEBUG
  5323. long maxSteps = path->init(numCells);
  5324. if (maxSteps > -1) {
  5325. File* pathDebugFile = new File;
  5326. pathDebugFile->create("longpath.dbg");
  5327. char s[512];
  5328. sprintf(s, "New Longest Path: %d steps (Max = %d)\n\n", numCells, MAX_STEPS_PER_MOVEPATH);
  5329. pathDebugFile->writeString(s);
  5330. writeDebug(pathDebugFile);
  5331. pathDebugFile->close();
  5332. delete pathDebugFile;
  5333. pathDebugFile = NULL;
  5334. }
  5335. #endif
  5336. path->target = target;
  5337. path->cost = map[bestRow * maxWidth + bestCol].g;
  5338. curRow = (long)bestRow;
  5339. curCol = (long)bestCol;
  5340. long curCell = numCells;
  5341. if (doorDirection == -1) {
  5342. if (goalWorldPos) {
  5343. goalWorldPos->x = (float)(ULc + bestCol) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  5344. goalWorldPos->y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + bestRow) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  5345. goalWorldPos->z = (float)0; // How do we get the elevation for this point? Do we care?
  5346. path->goal = *goalWorldPos;
  5347. }
  5348. else
  5349. path->goal = goal;
  5350. }
  5351. else {
  5352. //--------------------------------------------------
  5353. // It's a door, so it's the last on the path list...
  5354. curCell--;
  5355. path->setDirection(curCell, /*reverseShift[*/doorDirection * 2/*]*/);
  5356. long doorR = bestRow + adjTile[doorDirection][0];
  5357. long doorC = bestCol + adjTile[doorDirection][1];
  5358. goalCell[0] = ULr + doorR;
  5359. goalCell[1] = ULc + doorC;
  5360. Stuff::Vector3D stepDest;
  5361. stepDest.x = (float)(ULc + doorC) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  5362. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(ULr + doorR) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  5363. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  5364. path->setDestination(curCell, stepDest);
  5365. path->setDistanceToGoal(curCell, 0.0);
  5366. path->setCell(curCell, goalCell[0], goalCell[1]);
  5367. path->goal = stepDest;
  5368. if (goalWorldPos) {
  5369. //--------------------------------------------------------------------
  5370. // We didn't know the exact goal world pos coming into this, since are
  5371. // goal was an area door....
  5372. *goalWorldPos = stepDest;
  5373. }
  5374. }
  5375. //---------------------------------------------------------
  5376. // We need to set this table up our first time thru here...
  5377. static bool setTable = false;
  5378. if (!setTable) {
  5379. float cellLength = Terrain::worldUnitsPerCell * metersPerWorldUnit;
  5380. for (long i = 0; i < NUM_CELL_OFFSETS; i++) {
  5381. float distance = agsqrt( cellShift[i * 2], cellShift[i * 2 + 1] ) * cellLength;
  5382. cellShiftDistance[i] = distance;
  5383. }
  5384. setTable = true;
  5385. }
  5386. while ((curRow != startR) || (curCol != startC)) {
  5387. curCell--;
  5388. long parent = reverseShift[map[curRow * maxWidth + curCol].parent];
  5389. if (parent > 7)
  5390. path->setDirection(curCell, parent);
  5391. else
  5392. path->setDirection(curCell, parent);
  5393. Stuff::Vector3D stepDest;
  5394. long cell[2];
  5395. cell[0] = ULr + curRow;
  5396. cell[1] = ULc + curCol;
  5397. stepDest.x = (float)(cell[1]) * Terrain::worldUnitsPerCell + Terrain::worldUnitsPerCell / 2 - Terrain::worldUnitsMapSide / 2;
  5398. stepDest.y = (Terrain::worldUnitsMapSide / 2) - ((float)(cell[0]) * Terrain::worldUnitsPerCell) - Terrain::worldUnitsPerCell / 2;
  5399. stepDest.z = (float)0; // How do we get the elevation for this point? Do we care?
  5400. if (curCell == (numCells - 1) && (parent <= 7))
  5401. path->setDistanceToGoal(curCell, 0.0);
  5402. else
  5403. path->setDistanceToGoal(curCell, cellShiftDistance[path->getDirection(curCell + 1)] + path->getDistanceToGoal(curCell + 1));
  5404. path->setDestination(curCell, stepDest);
  5405. path->setCell(curCell, cell[0], cell[1]);
  5406. map[curRow * maxWidth + curCol].setFlag(MOVEFLAG_STEP);
  5407. long cellOffsetIndex = map[curRow * maxWidth + curCol].parent << 1;
  5408. curRow += cellShift[cellOffsetIndex++];
  5409. curCol += cellShift[cellOffsetIndex];
  5410. //calcAdjNode(curRow, curCol, map[curRow * maxCellWidth + curCol].parent);
  5411. }
  5412. #ifdef BLOCKED_PATH_TEST
  5413. //---------------------------------------------------------------------
  5414. // Let's test every path point to make sure it is not a blocked cell...
  5415. for (long i = 0; i < path->numSteps; i++) {
  5416. long tile[2], cell[2];
  5417. GameMap->worldToMapPos(path->stepList[i].destination, tile[0], tile[1], cell[0], cell[1]);
  5418. bool cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  5419. if (!cellPassable)
  5420. cellPassable = GameMap->cellPassable(tile[0], tile[0], cell[0], cell[1]);
  5421. Assert(cellPassable || (i < 3), 0, " Bad Path Point ");
  5422. }
  5423. #endif
  5424. }
  5425. #ifdef TIME_PATH
  5426. QueryPerformanceCounter(calcStop);
  5427. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  5428. if (debugger && (scenarioTime > 0.0)) {
  5429. char s[50];
  5430. sprintf(s, "path calc: %.4f\n", calcTime);
  5431. OutputDebugString(s);
  5432. debugger->print(s);
  5433. }
  5434. #endif
  5435. return(path->numSteps);
  5436. }
  5437. #ifdef TIME_PATH
  5438. QueryPerformanceCounter(calcStop);
  5439. calcTime = float(calcStop.LowPart - calcStart.LowPart) / float(countsPerSecond.LowPart);
  5440. if (debugger && (scenarioTime > 0.0)) {
  5441. char s[50];
  5442. sprintf(s, "path calc: %.4f", calcTime);
  5443. debugger->print(s);
  5444. }
  5445. #endif
  5446. return(0);
  5447. }
  5448. //---------------------------------------------------------------------------
  5449. void MoveMap::writeDebug (File* debugFile) {
  5450. char outString[512];
  5451. sprintf(outString, "Time = %.6f\n\n", calcTime);
  5452. debugFile->writeString(outString);
  5453. sprintf(outString, "Start = (%d, %d)\n", startR, startC);
  5454. debugFile->writeString(outString);
  5455. sprintf(outString, "Goal = (%d, %d)\n", goalR, goalC);
  5456. debugFile->writeString(outString);
  5457. strcpy(outString, "\n");
  5458. debugFile->writeString(outString);
  5459. debugFile->writeString("PARENT:\n");
  5460. debugFile->writeString("-------\n");
  5461. for (long r = 0; r < height; r++) {
  5462. outString[0] = NULL;
  5463. char numStr[10];
  5464. for (long c = 0; c < width; c++) {
  5465. /*if ((goalR == r) && (goalC == c))
  5466. sprintf(numStr, "G");
  5467. else*/
  5468. if ((startR == r) && (startC == c))
  5469. sprintf(numStr, "S");
  5470. else if (map[r * maxWidth + c].parent == -1)
  5471. sprintf(numStr, ".");
  5472. else if (map[r * maxWidth + c].flags & MOVEFLAG_STEP)
  5473. sprintf(numStr, "X");
  5474. else
  5475. sprintf(numStr, "%d", map[r * maxWidth + c].parent);
  5476. strcat(outString, numStr);
  5477. }
  5478. strcat(outString, "\n");
  5479. debugFile->writeString(outString);
  5480. }
  5481. debugFile->writeString("\n");
  5482. debugFile->writeString("MAP:\n");
  5483. debugFile->writeString("-------\n");
  5484. for (r = 0; r < height; r++) {
  5485. outString[0] = NULL;
  5486. char numStr[10];
  5487. for (long c = 0; c < width; c++) {
  5488. if ((goalR == r) && (goalC == c))
  5489. sprintf(numStr, "G");
  5490. else if ((startR == r) && (startC == c))
  5491. sprintf(numStr, "S");
  5492. else if (map[r * maxWidth + c].cost == clearCost)
  5493. sprintf(numStr, ".");
  5494. else if (map[r * maxWidth + c].cost >= COST_BLOCKED)
  5495. sprintf(numStr, " ");
  5496. else if (map[r * maxWidth + c].cost < 256)
  5497. sprintf(numStr, "%x", map[r * maxWidth + c].cost);
  5498. strcat(outString, numStr);
  5499. }
  5500. strcat(outString, "\n");
  5501. debugFile->writeString(outString);
  5502. }
  5503. debugFile->writeString("\n");
  5504. debugFile->writeString("PATH:\n");
  5505. debugFile->writeString("-------\n");
  5506. for (r = 0; r < height; r++) {
  5507. outString[0] = NULL;
  5508. char numStr[10];
  5509. for (long c = 0; c < width; c++) {
  5510. if ((goalR == r) && (goalC == c))
  5511. sprintf(numStr, "G");
  5512. else if ((startR == r) && (startC == c))
  5513. sprintf(numStr, "S");
  5514. else if (map[r * maxWidth + c].flags & MOVEFLAG_STEP)
  5515. sprintf(numStr, "*");
  5516. else if (map[r * maxWidth + c].cost == clearCost)
  5517. sprintf(numStr, ".");
  5518. else if (map[r * maxWidth + c].cost >= COST_BLOCKED)
  5519. sprintf(numStr, " ");
  5520. else
  5521. sprintf(numStr, "%d", map[r * maxWidth + c].cost);
  5522. strcat(outString, numStr);
  5523. }
  5524. strcat(outString, "\n");
  5525. debugFile->writeString(outString);
  5526. }
  5527. debugFile->writeString("\n");
  5528. }
  5529. //---------------------------------------------------------------------------
  5530. void MoveMap::destroy (void) {
  5531. if (map)
  5532. {
  5533. systemHeap->Free(map);
  5534. map = NULL;
  5535. }
  5536. if (mapRowStartTable)
  5537. {
  5538. systemHeap->Free(mapRowStartTable);
  5539. mapRowStartTable = NULL;
  5540. }
  5541. if (mapRowTable)
  5542. {
  5543. systemHeap->Free(mapRowTable);
  5544. mapRowTable = NULL;
  5545. }
  5546. if (mapColTable)
  5547. {
  5548. systemHeap->Free(mapColTable);
  5549. mapColTable = NULL;
  5550. }
  5551. }
  5552. //***************************************************************************