Udawalawe National Park, Sri Lanka - It is difficult to predict when the elephants will come.

As darkness falls around Sri Lanka's Udawalawe National Park, the 52 villages that speckle its borders go on alert. Thin wire fences hum with the threat of electricity.

Across the dry zone, farmers climb up into rudimentary treehouses overlooking their paddy fields. They must try to stay alert as the darkness deepens. Armed only with torches, fireworks, and their voices - loud and hoarse - they may be forced to face down giants.

Ashoka Ranjeewa, an elephant researcher, has spent many nights out here. When he first arrived in Pokunuthanna, there were few friendly faces. Bordered on two sides by the national park, and on one side by the Dahaiyagala sanctuary, this village of some 100 families has seen more than its fair share of elephant attacks. Farmers here allege the compensation they are paid is meagre and comes late. Outsiders only come to gawk, taking pictures, commiserating.

None of this stops the elephants from coming.

Killer elephants

The foragers are most often male elephants - bulls - on average weighing in at 5,000-6,000 kilogrammes, and reaching over three metres tall, these are among the largest land animals alive.

The villagers of Pokunuthanna used to believe just four to five bulls took turns invading their land. But when Ranjeewa installed some infrared night vision cameras, he raised the count to 35.

A single animal may still be scared off, but a bull group of three or more is simply terrifying. It can be suicide to stand in their way. The marauders can devastate a family's fortunes in one night: a full season's harvest torn up by the roots; a house holding a family's entire store of paddy raided, its clay walls knocked over and its roof trampled.

"People invest most of their money to buy seeds and agricultural chemicals," PP Ariyaratna, a local farmer, tells Al Jazeera. He explains big crop losses can send them into debt or force them into tenuous jobs as wage labourers.

Just last week, elephants visited Ariyaratna's paddy fields multiple times. In one case, circles of devastation mark the spot where they feasted. In another, an elephant reached sneakily under a live fence to pull paddy out by the roots.

Ariyaratna knows what they are capable of - he has woken to find a long trunk testing the integrity of the walls that shelter his family.

The farmer lives encircled by a ring of electricity but says the animals are adapting. "They are clever and smart," he says, explaining that he now tries to clear the land around his fences so there is no stick or branch lying around. Elephants have learned how to use these tools to break the wires.

Ariyaratna has a long list of other techniques that used to work, but aren't reliable any more. Ambulance lights that flash red and blue, recorded sounds of other animals, firecrackers, torches: elephants would be frightened initially, but all too soon returned to raiding.

They even seem to recognise when a fence is live - perhaps sensing a slight heat or an electric charge - and they know when a power cut has rendered the wires ineffectual. When a fence is torn down it might take days to repair, and the farms are defenceless all the while.

Like everyone else in this village, Ariyaratna knows someone who was killed: most often it is a man, who, walking down these dirt roads one night, came face to face with an elephant in the dark.

In 2016, elephants killed 88 people in Sri Lanka, according to Department of Wildlife Conservation statistics.

Read more about the conflict between humans and animals in this article titled, Hunting endangered animals in the jungles of Myanmar

Elephants in hostile territory

They are so intelligent. They take risks and they are bold. They have been deprived of their lands, surrounded by electric fences, and shot at. Still, they do not give up. Ashoka Ranjeewa, researcher

Vijitha Perera runs the Elephant Transit Home located inside the Udawalawe National Park. He knows what happens along the borders of the park.

"It is complicated because people and elephants are trying to share the same resources," he says. "In the elephant scenario, there is no idea of private property. People think the land is theirs, the water and crops are theirs. But elephants don't think like that."

Currently, a drought has left all the smaller watering holes depleted. The animals come out of the park, desperate for food.

"Their habitat is completed changed," says Ranjeewa, who also studies the animals inside the park. "Their scrub lands have been covered with invasive species like lantana." The farmers' paddy, on the other hand, is a rich source of nutrition.

As they roam outside the boundaries at night, Ranjeewa says he can see the stress on the elephant's faces. They know they are in hostile territory and make as little noise as possible.

His research has given Ranjeewa a deep admiration for these animals. "They are so intelligent. They take risks and they are bold. They have been deprived of their lands, surrounded by electric fences, and shot at. Still, they do not give up."

Ranjeewa grew up near Udawalawe and has seen the human elephant conflict escalate over the years. He knows of the violence against the elephants: they are poisoned, electrocuted and wounded by bullets. Planks studded with nails are left on jungle paths. Haka patta, small explosives, are camouflaged in food.

Elephant calves are most likely to bite into such a temptation. When the bomb goes off, it has just enough force to shatter the jawbone. No longer able to eat, death comes at the end of a long nightmare of infection and starvation.

In 2016, people killed 279 elephants in Sri Lanka, the Department of Wildlife Conservation statistics show.

Young victims of violence

Perera welcomes baby elephants at the Elephant Transit Home. The calves are usually very close to collapse - always orphans, separated from their mother either by death or chaos. The calves are too young to know how to avoid the dangers of a deep well or irrigation canal. People bring noise and terror, and they hide.

"When we find the babies, they are usually weak. They've undergone severe stress, dehydration and starvation," says Perera. "Some have infectious wounds and serious parasitic infections. Most are close to death."

Perera welcomes these orphans to the Elephant Transit Home, the only one of its kind in Sri Lanka. Here, the animals receive tender care. As they grow into adults, human contact is kept to a minimum.

"They are introduced into a herd," says Perera. The babies are fed nutritious milk, but otherwise the herd is left to its own devices. Each animal is welcomed into a complex social structure and they learn from each other.

"They find their own food and water, and when they turn five years old we release them back into the wild."

In their time at the centre, the young calves form deep attachments. "It is like a classroom, and they all have their friends. They form a social network and show no aggression toward each other."

The researchers identify these little units of camaraderie and release them together so they can forge a new life in the wild. They track them carefully to ensure their survival.

"Our first birth in the wild was in 2008," says Perera, adding that they have released 110 animals in total and seen 17 births in the wild.

Elephants a resource, not a pest

Prithiviraj Fernando scrambles down a slope into a field cultivated by a farmer's society in Orukemgala, Mattara. The chairman of the Centre for Conservation and Research, Fernando has helped set up a solar-powered electric fence here.

Unlike the so-called administrative fences that surround parks and are the property of the departments of Wildlife Conservation and Forests, this is a privately constructed ecological fence that protects just this patch of about three hectares of farmland. This distinction is key, says Fernando.

"Sri Lanka has more than 3,500km of fencing put up by the Wildlife Department and a lot of it is not working. The main problem is that it is in the wrong place."

In comparison, the location of this ecological fence has been carefully chosen with easy maintenance in mind. The farmers watching over it say it is the one thing that has allowed them to cultivate on this land for the last three years.

The solar panel powers not just the fence, but ensures that the posts are also charged, so they can't be uprooted or kicked over. In a programme run in collaboration with the Department of Wildlife Conservation, 10 such fences are being trialled across villages and farms in the south. They seem to be working. "A successful solution had to be simple," says Fernando.

Find out more about the careful balance required for harmonious cohabitation with animals: Should you swim with whale sharks in the Philippines?

Fernando has spent years immersed in the human-elephant conflict. By putting radio collars around elephants and tracking them, Fernando and other researchers were able to establish that the vast majority of elephants in Sri Lanka actually roamed outside the areas protected by the Wildlife Department.

This is crucial considering Sri Lanka's preference for translocation as a solution to conflict. Translocation is the method adopted when large development or irrigation projects are scheduled or land is being claimed for farming. Elephant drives are organised to chase the animals out of the area.

"You have hundreds of people going into the forest and creating a huge din, lighting thousands of crackers," says Fernando. "Sometimes, this goes on for days, sometimes for months, something for over a year."

What the researchers found was that try as people might, they never succeeded in driving away all the elephants. Moreover, those who were forcibly translocated to national parks, do not stay in the parks - instead they try to return or wander elsewhere, raiding new farms.

They tend to become more aggressive and human deaths that resulted were usually an accident. Fernando believes that "80 percent of deaths can be avoided," saying that it is clear translocation moves not just the elephant, but the problem as well.

Looking at this fence in Hambantota, Fernando doesn't think the solution lies in either moving people or elephants. Instead, it is in finding ways for them to co-exist right where they are.

Back in Pokunuthanna, Ranjeewa has been making efforts to help people understand why the animals behave as they do through discussions at the Dahaiyagala temple, house-to-house visits and the formation of a Young Conservationists Society. Ranjeewa has pushed local authorities to install street lights to help prevent people running into the animals at night.

In fact, his friend Ajith Sandanayake has decided to see how the village can profit from the nightly incursions - he has guests from all over come to spend the night at his ecolodge. There is even a treehouse. On those days, he actually hopes the elephants will come to visit.

"We have to convert the farmer's attitudes," says Sandanayake, "They have to start seeing the elephant not as a pest, but as a resource."

He describes how the whole village turns out to marvel when a particularly large herd gathers at a lake just across them, within the borders of the national park. "We all go to look at them. The truth is people do not hate the elephants. Like us, we know they also need to eat."