My bright yellow helicopter swoops low over one of Rio's 600 or more favelas, the slums that house some of the most violent, heavily armed gangsters in the world. For all his many years flying choppers in the city where he was born and raised, Ricardo, my pilot, has never been over these areas, let alone at such a low altitude: we are about 50 metres above the mess of jerry-built brick and concrete houses, the tangled wires of stolen electricity, and the pot-holed streets of Coreia, where 150,0000 people try to survive grinding poverty and the urban war that surrounds them. The drug lord who controls Coreia is one of the main characters in my film, Dancing with the Devil, a sort of City of God meets The Wire – but all for real.

Ricardo is understandably nervous. The previous Christmas, some bright spark had the idea of flying Father Christmas over the city in a helicopter to deliver gifts to kids in a slum; no one told the gangsters and the chopper was fired on. "Are you sure you have cleared this with the traficantes?" Ricardo's voice crackles on the radio over the racket of the motor. The answer is a firm "Yes" as I would never be so foolhardy as to go anywhere near any of these places – whether in the air or on the ground – without firm guarantees for my safety from the gangsters who are the de facto authorities in these slums. They house an estimated 20 per cent of the 11.7 million souls who call metropolitan Rio de Janeiro home – that's nearly two and a half million people, all living under the rule of the semi-automatic machine gun of the traffickers.

Memories of my chopper ride with Ricardo came flooding back last weekend as news came through of gun battles in Rio's Morro dos Macacos – Monkey Hill – slum. To date, 21 people have been killed, three of whom were military policemen who died as a result of their helicopter plunging to earth after the pilot had been shot in the leg by traffickers.

Download the new Independent Premium app Sharing the full story, not just the headlines

During filming, my crew and I accompanied more than 850 police from all of Rio's specialist units as they made their presence felt in the Complexo do Alemao (the German Complex), a massive network of 13 favelas under the control of one of Rio's three warring gangs, the Red Command. This was the first time the police had gone into the favela in nearly 15 months, and, as Inspector Jacyr dos Santos Jnr of the drug squad told us, the traffickers, if anything, had reinforced their control over the area since then.

The new mission was to recover the bodies of some 30 traffickers allegedly murdered and thrown on a bonfire by rivals in their own gang. In the operation, two policemen lost their lives, one only yards from us – hit in the head by a sniper who may even have been another cop mistaking him for a trafficker. Two alleged traffickers also lost their lives, and another policeman and a female favela resident were wounded. The charred remains of four traffickers were recovered on a smouldering bonfire but never identified.

And all this in a place which tops a recent Forbes list of the world's happiest cities. Admittedly the survey was based on the perception of Rio by people around the world as a "happy" place, with its famous "Carnaval" and sun-drenched beaches of Copacabana and Ipanema, rather than the views of the citizens of the German Complex or Coreia.

Therein lies the heart of the dilemma when dissecting Rio. It is, essentially, two cities, not only cheek by jowl, but intimately and inextricably intertwined through a combination of history, geography and economics. Great wealth alongside great poverty is not a unique phenomenon in the developing world, but Rio sits at the extreme. For one thing, the city's unique topography with mountains springing out from the sea, means that slums are right in amongst the wealthy areas, as well as stretching out from the centre in the more usual pattern of conurbations in the Third World. So Rio's south side, encompassing the modern apartment blocks and beaches so familiar to anyone who has ever seen a tourist poster of the city, or the more recent nouveau riche modern developments of Barra da Tijuca, are situated just metres from extreme poverty.

Then, as you move out from the centre, there lies the ruined ambitions of previous failed efforts at social engineering. Places like Vila Kennedy, a product of aid from the Alliance for Progress which poured $20bn (£12bn) into Latin America in its 10-year existence as an anti-Communist initiative set up by the US President after whom the slum is named. Vila Kennedy may boast its own scaled-down replica of the Statue of Liberty, but there is nothing else that could remotely be called picturesque in this place of crumbling concrete houses, streets littered with trash, and all the other familiar signs of urban ruin.

It was once the childhood home of Eduardo, the Brazilian, now Croatian, international striker who plays for Arsenal. He may well ply his trade in the red colours of the Gunners, but the young men he grew up with are for the most part members of a less salubrious club, as foot-soldiers in the Red Command drug gang.

After the early enthusiasm for social reform, which led to the creation of places like Vila Kennedy where the residents of Rio's inner city slums were to be re-housed 30km or more away from the city's original heart, the state then paid little attention to them. Gradually drugs, and in particular cocaine, played a greater part in the lives of these people, and with the drugs came the gangs. And with the gangs came gang warfare. And with the gang warfare came war between the gangsters and the police.

Daily coronavirus briefing No hype, just the advice and analysis you need Enter your email address Continue Continue Please enter an email address Email address is invalid Fill out this field Email address is invalid Email already exists. Log in to update your newsletter preferences Register with your social account or click here to log in I would like to receive morning headlines Monday - Friday plus breaking news alerts by email Update newsletter preferences

The first serious gang, the Red Command, which is still Rio's largest, started life in the prisons of the city during the right-wing military dictatorship that ruled the country from 1964 to 1985. In the late Seventies the regime imprisoned a large number of leftist opponents, incarcerating them with common criminals. The former proceeded to "politicise" the latter whilst adopting the criminality of their new best friends. The sale of drugs, as is often the way with such organisations, provided their source of income, and gradually became their entire raison d'être, with all political notions abandoned.

As time went by, the Red Command split and, in the mid-1980s a new group, the Amigos dos Amigos (the Friends of Friends), more usually known by their initials, the ADA, emerged from the power struggle. In 1994 a further split in the Red Command led to the emergence of the Third Command but, largely as a result of internecine rivalry between the gangsters, this group has since been supplanted by the Pure Third Command. Just to add spice to this mix, a fourth group of paramilitaries, often ex-soldiers or even serving off-duty policemen, operate in their favela strongholds through protection rackets and indiscriminate violence. And finally, of course, a layer of corrupt cops provides another level of violence and money-making.

All this means that any ordinary person who simply wants to get on with their life has to negotiate a veritable minefield, in some cases quite literally. Five years ago, for example, in a stronghold of the Pure Third Command, police discovered land mines, bazookas and an anti-aircraft machine gun. Even though I have covered wars in the Middle East, Asia and Africa, I have never seen so many high-powered automatic weapons as I witnessed then being carried openly in the streets by civilians in what is ostensibly not a war zone.

Today the state has largely lost control of the slums as the traffickers enforce their monopoly over the drug trade with force. In effect the gangs operate as businesses, with a weekly payroll for employees and a strictly hierarchical structure. At the bottom are the boys with walkie-talkies, who patrol the streets as a first line of defence to alert their superiors to any impending police action or other potential disturbance. Then there are those who work indoors, sorting, diluting and packing the marijuana or cocaine into single-unit packs for sale at negligible prices on the street. There are of course also the sellers, the soldiers, the accountants and, at the top, the bosses.

For all the money involved, these drug lords have little in common with their more famous fellows in Columbia. Rio's drug bosses live a life on the run in the slums they control; death stalks the streets alongside them, whether from the weapon of a cop or a rival trafficker. The Brazilian traffickers are not exporters, where the real money is made, but importers, from Bolivia and Paraguay mostly. Sure, they may be able to afford a plasma television, or even a couple of cars, but this is a far cry from the world of Colombia's Pablo Escobar with his mansions and exotic menageries.

And the war on the ground is a brutal one, as gangs vie for territorial control. It was, by all accounts, an attempted takeover that precipitated last weekend's paroxysm of lethal violence as a group of Red Command traffickers tried to seize control from elements of the ADA in the Morro dos Macacos favela in the city's north zone close to the Maracana football stadium that will play host to both the 2014 World Cup and also various events of the 2016 Olympics. In the gun battle, and the subsequent police intervention, some of the bloodiest violence in the city for some time took place.

Inevitably, everyone wants to know what the implications for the Olympics of such violence might be. My own view is that there are no implications. The Pan American Games passed off relatively peacefully, in no small part because the traffickers operate in their own fiefdom and have no interest in either spreading their role to embrace the temporary visitors, nor have they any interest in bringing upon themselves the inevitable storm-troop response of a militarised police force keen to make a point in the face of adverse publicity about security. The fact the streets were flooded with police for the fortnight of the 2007 Games also counted for something of course, and no doubt both 2014 and 2016 will be characterised by a similar police presence.

Yes, there will be muggings at the World Cup and the Olympics, but these are not the product of gangsterism, but the normal day-to-day freelance activities of petty criminals. Yes, they may be feeding a drug habit and yes, they may be trading their stolen goods in the drug dens of the favelas, but this is not where the heart of Rio's violence lies.

That is firmly set in a much deeper social malaise: a state that has adopted the "solution" of a low-level war to fight what is, in fact, the product of deep socio-economic problems, of which the drugs trade and gangsterism are the most obvious symptoms. Rather than rethink the whole approach to drugs with no potential answer, including wholesale legalisation, left unexamined, the state seems prepared to accept that a war that leaves more than 1,000 civilians and countless traffickers dead each year, together with as many as 500 policemen killed on- and off-duty, is an acceptable price to pay for the status quo.

The choice that now faces Rio's policymakers is simple. Either they use the opportunity offered by the Olympics to think outside of the box, or they can tinker at the edges. Worse still, they can simply go on as they have with the military "solution", throwing large numbers of heavily armed police into the favelas on raids that will kill a few traffickers as well as a few innocents, only for another generation of drug lords to take their place. With President Lula's announcement on Monday that he is going to allocate 100m reais (£34.8m) from the federal government to Rio's police, there is little sign that a fresh approach is even on the radar.

If that is the case, little is likely to change and there will simply be more killing on all sides. As one senior cop in the drug squad said: "The traffickers don't take anyone by force and compel them to consume [drugs]. But they sell, and if they are selling, there is someone who is buying... someone out there on the beach, right next to us, sometimes in your own family."

Later this same cop was to turn to me just before we came under heavy fire in a narrow favela alley, to say: "In your country what we do here is called madness. As the younger guys say – 'This shit is crazy'."

Jon Blair's new film, Dancing with the Devil, will be shown on More 4 on 10 November at 10pm