Commit 65be5130 authored by Alexander Schuch's avatar Alexander Schuch
Browse files

Update source code formatting.

The source code uses many different formatting styles. This is a very
early attempt to harmonise the formatting: Remove trailing spaces, put
method return data type on a separate line, remove spaces right after
and right before brackets and so on.
parent 2bb909c2
......@@ -25,17 +25,22 @@
#include "../planet.h"
#include <cmath>
//---------------------------------------------------------------------------
// class Map
//---------------------------------------------------------------------------
Map::Map(int rowsCount, int colsCount)
: m_rows( rowsCount ),
m_columns( colsCount )
Map::Map(int rowsCount, int colsCount) :
m_rows(rowsCount),
m_columns(colsCount)
{
resizeMap(rowsCount, colsCount);
}
void Map::resizeMap (int rowsCount, int columnsCount)
Map::~Map()
{
}
void
Map::resizeMap (int rowsCount, int columnsCount)
{
m_rows = rowsCount;
m_columns = columnsCount;
......@@ -43,95 +48,100 @@ void Map::resizeMap (int rowsCount, int columnsCount)
m_grid = QList<QList<Sector> >();
// Initialize the grid of Sectors.
for( int row = 0; row < rowsCount; row++ ) {
for (int row = 0; row < rowsCount; row++) {
m_grid << QList<Sector>();
for( int col = 0; col < columnsCount; col++ ) {
m_grid[row] << Sector( this, Coordinate( row, col ) );
connect( &m_grid[row][col], SIGNAL( update() ),
this, SLOT( childSectorUpdate() ) );
for (int col = 0; col < columnsCount; col++) {
m_grid[row] << Sector(this, Coordinate(row, col));
connect(&m_grid[row][col], SIGNAL(update()), this, SLOT(childSectorUpdate()));
}
}
}
Map::~Map()
{
}
void
Map::addPlanet(Sector *sector, Player *player, int production, double killpercentage)
{
new Planet( UniquePlanetName(), sector, player, production, killpercentage);
new Planet(UniquePlanetName(), sector, player, production, killpercentage);
}
Planet*
Map::addPlayerPlanetSomewhere(Player *player)
{
Sector *sector = findRandomFreeSector();
if(!sector)
if (!sector)
return NULL;
return Planet::createPlayerPlanet(sector, player, UniquePlanetName());
}
Planet*
Map::addNeutralPlanetSomewhere(Player *neutral)
{
Sector *sector = findRandomFreeSector();
if(!sector)
if (!sector)
return NULL;
return Planet::createNeutralPlanet(sector, neutral, UniquePlanetName());
}
bool
Map::removePlayerPlanet(Player *player)
{
foreach(Planet *planet, planets())
if(planet->player() == player) {
foreach (Planet *planet, planets()) {
if (planet->player() == player) {
delete planet;
return true;
}
}
return false;
}
void
Map::removePlayerPlanets(Player *player)
{
while(removePlayerPlanet(player)) ;
}
void
Map::turnOverPlayerPlanets(Player* owner, Player* newOwner)
{
foreach(Planet *planet, planets())
{
if(planet->player() == owner) {
foreach (Planet *planet, planets()) {
if (planet->player() == owner) {
planet->setOwner(newOwner);
}
}
}
int
Map::playerPlanetCount(Player *player)
{
int count = 0;
foreach(Planet *planet, planets())
if(planet->player() == player)
foreach (Planet *planet, planets()) {
if (planet->player() == player) {
count++;
}
}
return count;
}
void
Map::clearMap()
{
int x,y;
for( x = 0; x < rows(); ++x )
for( y = 0; y < columns(); ++y )
for (int x = 0; x < rows(); ++x) {
for (int y = 0; y < columns(); ++y) {
delete m_grid[x][y].planet();
}
}
emit update();
}
QString
Map::UniquePlanetName(void)
{
......@@ -146,62 +156,77 @@ again:
return QChar(c);
}
void
Map::populateMap( const QList<Player *> &players, Player *neutral,
int numNeutralPlanets)
{
Map::populateMap(const QList<Player *> &players, Player *neutral, int numNeutralPlanets)
{
// Create a planet for each player.
foreach(Player *player, players)
foreach (Player *player, players) {
addPlayerPlanetSomewhere(player);
}
for( int x = 0; x < numNeutralPlanets; x++ ) {
for (int x = 0; x < numNeutralPlanets; ++x) {
Sector *sector = findRandomFreeSector();
if(sector)
Planet::createNeutralPlanet( sector, neutral, UniquePlanetName() );
if (sector) {
Planet::createNeutralPlanet(sector, neutral, UniquePlanetName());
}
}
emit update();
}
double Map::distance( Planet *p1, Planet *p2 )
double
Map::distance(Planet *p1, Planet *p2)
{
Coordinate diff = p1->sector()->coord() - p2->sector()->coord();
return sqrt( double( ( diff.x() * diff.x() )
+ ( diff.y() * diff.y() ) ) )/2; // Yes, we're dividing by two. It's not a bug, it's a feature.
return sqrt(double((diff.x() * diff.x()) + (diff.y() * diff.y()))) / 2; // Yes, we're dividing by two. It's not a bug, it's a feature.
}
Sector *Map::findRandomFreeSector()
{
foreach(const QList<Sector> &i, m_grid)
foreach(const Sector &j, i)
if(!j.hasPlanet())
Sector*
Map::findRandomFreeSector()
{
foreach (const QList<Sector> &i, m_grid) {
foreach (const Sector &j, i) {
if (!j.hasPlanet()) {
goto freesectorexists;
}
}
}
return NULL;
freesectorexists:
Coordinate c;
do {
c = Game::generatePlanetCoordinates( rows(), columns() );
} while( m_grid[c.x()][c.y()].hasPlanet() );
c = Game::generatePlanetCoordinates(rows(), columns());
} while (m_grid[c.x()][c.y()].hasPlanet());
return &m_grid[c.x()][c.y()];
}
void Map::childSectorUpdate()
void
Map::childSectorUpdate()
{
emit update();
}
QList <Planet*> Map::planets()
QList <Planet*>
Map::planets()
{
QList <Planet*>planets;
foreach(const QList<Sector> &i, m_grid)
foreach(const Sector &j, i)
if(j.hasPlanet())
planets += j.planet();
foreach (const QList<Sector> &i, m_grid) {
foreach (const Sector &j, i) {
if (j.hasPlanet()) {
planets += j.planet();
}
}
}
return planets;
}
......@@ -19,18 +19,16 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KONQUEST_MAP_H
#define KONQUEST_MAP_H
#include <QObject>
#include <QList>
#include "sector.h"
#include "../players/player.h"
//*****************************************************************
// class Map
//*****************************************************************
class Map : public QObject
{
......@@ -43,31 +41,29 @@ public:
int rows() { return m_rows; }
int columns() { return m_columns; }
void addPlanet(Sector *sector, Player *player, int production, double killpercentage);
Planet* addPlayerPlanetSomewhere(Player *player);
Planet* addNeutralPlanetSomewhere(Player *neutral);
bool removePlayerPlanet(Player *player);
void removePlayerPlanets(Player *player);
int playerPlanetCount(Player *player);
void turnOverPlayerPlanets(Player* owner, Player* newOwner);
void addPlanet(Sector *sector, Player *player, int production, double killpercentage);
Planet* addPlayerPlanetSomewhere(Player *player);
Planet* addNeutralPlanetSomewhere(Player *neutral);
bool removePlayerPlanet(Player *player);
void removePlayerPlanets(Player *player);
int playerPlanetCount(Player *player);
void turnOverPlayerPlanets(Player* owner, Player* newOwner);
void clearMap();
void resizeMap(int rows, int cols);
void clearMap();
void resizeMap(int rows, int cols);
void populateMap( const QList<Player *> &players, Player *neutral,
int numNeutralPlanets);
double distance( Planet *p1, Planet *p2 );
void populateMap( const QList<Player*> &players, Player *neutral, int numNeutralPlanets);
Sector *sector( Coordinate coord )
double distance( Planet *p1, Planet *p2 );
Sector *sector(Coordinate coord)
{
if (coord.y() >= 0 && coord.y() < m_grid.size())
{
if (coord.x() >= 0 && coord.x() < m_grid[coord.y()].size())
{
if (coord.y() >= 0 && coord.y() < m_grid.size()) {
if (coord.x() >= 0 && coord.x() < m_grid[coord.y()].size()) {
return &m_grid[coord.y()][coord.x()];
}
}
return NULL;
}
......@@ -80,9 +76,9 @@ signals:
void update();
private:
Sector *findRandomFreeSector();
QString UniquePlanetName(void);
Sector* findRandomFreeSector();
QString UniquePlanetName(void);
protected:
// A map is a 2-D array of Sectors.
QList<QList<Sector> > m_grid;
......
......@@ -20,6 +20,7 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "minimapview.h"
#include <QMouseEvent>
......@@ -30,18 +31,20 @@
#include "players/player.h"
MiniMapView::MiniMapView( QWidget *parent )
: QWidget( parent ),
m_map( 0 ), m_selection(-1, -1)
MiniMapView::MiniMapView(QWidget *parent) :
QWidget(parent),
m_map(0),
m_selection(-1, -1)
{
QPalette pal = palette();
pal.setColor( backgroundRole(), Qt::black );
setPalette( pal );
pal.setColor(backgroundRole(), Qt::black);
setPalette(pal);
setMinimumSize( 100, 100 );
setMinimumSize(100, 100);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
MiniMapView::~MiniMapView()
{
}
......@@ -51,10 +54,12 @@ void
MiniMapView::setMap(Map *map)
{
m_map = map;
connect( m_map, SIGNAL( update() ), this, SLOT( update() ) );
connect(m_map, SIGNAL(update()), this, SLOT(update()));
}
void MiniMapView::CalculateOffsets (float &sectorSize, float &woffset, float &hoffset)
void
MiniMapView::CalculateOffsets(float &sectorSize, float &woffset, float &hoffset)
{
sectorSize = ((float)width())/m_map->columns();
if (height()/m_map->rows() < sectorSize)
......@@ -64,7 +69,9 @@ void MiniMapView::CalculateOffsets (float &sectorSize, float &woffset, float &ho
hoffset = ((float)height() - m_map->rows()*sectorSize)/2;
}
void MiniMapView::mousePressEvent ( QMouseEvent * event )
void
MiniMapView::mousePressEvent(QMouseEvent *event)
{
float sectorSize, woffset, hoffset;
CalculateOffsets(sectorSize, woffset, hoffset);
......@@ -79,7 +86,9 @@ void MiniMapView::mousePressEvent ( QMouseEvent * event )
emit sectorSelected(m_selection);
}
void MiniMapView::paintEvent(QPaintEvent * /*event*/)
void
MiniMapView::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
......@@ -114,11 +123,12 @@ void MiniMapView::paintEvent(QPaintEvent * /*event*/)
painter.setBrush(player->color());
// Draw a circle in the planets color to show the planet.
painter.drawEllipse(
QRectF(woffset + col * sectorSize,
hoffset + row * sectorSize,
sectorSize,
sectorSize));
painter.drawEllipse(QRectF(
woffset + col * sectorSize,
hoffset + row * sectorSize,
sectorSize,
sectorSize
));
}
}
}
......
......@@ -20,10 +20,10 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KONQUEST_MINIMAPVIEW_H
#define KONQUEST_MINIMAPVIEW_H
#include <QWidget>
#include "map/map.h"
......@@ -34,10 +34,10 @@ class MiniMapView : public QWidget
Q_OBJECT
public:
explicit MiniMapView( QWidget *parent = 0 );
explicit MiniMapView(QWidget *parent = 0);
~MiniMapView();
void setMap( Map *newMap );
void setMap(Map *newMap);
/**
* @note Always use hasSelection() first to make sure that the returned
......@@ -52,15 +52,14 @@ signals:
void sectorSelected(const Coordinate &coord);
protected:
void mousePressEvent( QMouseEvent * event );
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
void CalculateOffsets (float &, float &, float &);
void CalculateOffsets(float &, float &, float &);
Map *m_map;
Coordinate m_selection;
};
#endif // KONQUEST_MINIMAPVIEW_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment