The Andalusian town of Setenil de las Bodegas (Sierra de Cádiz) looks like something from the set of a culturally adventurous fantasy film, but cave dwelling is actually common in Andalucia; being so close to the African continental plate, geological forces threw up mountain ranges and volcanoes that are perfectly suited for habitation. The rocks and caves make for surprisingly easy settlement, and the nearby Cueva de la Pileta shows evidence of a human presence up to 25,000 years ago. After that, though, the inhabitants of the caves here are mostly ignored up until the 12th century, when Setenil's Moorish castle appears on the historical record.
More comprehensive use of the geology as a place to live only appears in Moorish Spain, rather than earlier - though it's easy to assume this living arrangement was a local tradition that the Moors imitated, it's actually a survivor of a much older Arabic tradition of underground and cave dwelling, a strategy to compensate for the extreme heat in parts of the Arabic world. It's understandable that the tradition survived past the end of Muslim rule, too: caves are cheap to make habitable, climate-controlled, unlikely to burn down, earthquake resistant and, due to their thousand year history in Arabic cultures, are usually converted by people who know how to use light wells or purpose built light ducts, making the most of the available light with techniques such as white wall finishes.
Building in and around these huge outcroppings of rock may seem outlandish, but really it's a very practical decision taken by people who understood how best to take advantage of this terrain. Given the unfortunately hostile relations between medieval Islamic Spain and its northern, Christian neighbors, it was also very important strategically; the 12th century castle at the top of the town was referred to as nigh-impregnable by contemporary Catholic chroniclers, and blocked the Christian advance to Granada for about 50 years.
Although Setenil often attracts attention and tourism for its spectacular setting, it teaches us an important lesson thanks to how normally the town, and other cave dwellings in the area, are treated by the inhabitants. These incredibly efficient, low impact dwellings have been in use for 500 years or longer, and are a wonderful example of how a feature that would normally impede settlement can be turned into a huge advantage for those who live there.