Well a compression test couldnt hurt, but i wouldnt be TOO worried about 166k.
I wouldnt want to give you an estimate of how much a replacement engine would cost, but it should be fairly cheap, there are a lot of them out there.
As far as trans and transfer case, i got an aw4 and np231 at the pull a part for soemthing like $120.
My XJ has 142k and i have had close to ZERO trouble out of it, quite honestly the most reliable car ive owned. My MJ has 206k on the clock and i am still not afraid to bounce off the rev limiter like it owes me something. It may blow up at any minute, but it hasnt yet? The MJ does have some blowby.
If that other XJ doesnt work out, Im starting to consider selling mine. Let me know if you want specs. Pretty much stock daily driver, 2 inch lift, auto, same price range.