I think finished structures

and some changes in loading
and changes in map class
we have 2 TODO now
This commit is contained in:
2010-08-22 21:55:29 +03:00
parent f6125014dd
commit 191e5ed1a1
14 changed files with 178 additions and 133 deletions

172
map.cpp
View File

@@ -1,18 +1,30 @@
#include "map.h"
Map::Map(int Id, QObject *parent) :
Map::Map(QByteArray data, QSize size, QString name, int maxPlayers, int image, QObject *parent) :
QObject(parent)
{
mapId=Id;
}
void Map::load()
{
//ReadSettings();
//CreateMapExample();
QByteArray ba = QByteArray::fromBase64(data);
ba = qUncompress(ba);
QDataStream s(ba);
mapName = name;
s >> Cells;
int chei;
if (Cells.size() <= 0)
qFatal("Map has invalid width");
if (Cells.size() != size.width())
qCritical("Map has wrong width");
mapSize.setWidth(Cells.size());
chei = Cells.at(0).size();
for (int i=0; i<Cells.size(); i++)
{
if (Cells[i].size() <= 0)
qFatal("Map has invalid height");
if (Cells[i].size() != size.height())
{
qCritical("Map has wrong height");
if (Cells[i].size() != chei)
qFatal("Corrupted map data");
}
for (int j=0; j<Cells[i].size(); j++)
{
if (Cells[i][j]==Start) starts.push_back(QPoint(i,j));
@@ -20,29 +32,32 @@ void Map::load()
}
}
if (starts.isEmpty() || finishs.isEmpty()) qFatal("invalid map, not found start/finish");
// TODO: check for maxPlayers
players = maxPlayers;
imageType = image;
}
void Map::ReadSettings()
{
QString str;
SettingsReader * sreader = new SettingsReader("map.conf");
str = sreader->getValue("cells path");
maxPlayers = sreader->getValue("max players","1").toInt();
QFile * f = new QFile(str);
f->open(QIODevice::ReadOnly);
QDataStream s(f);
s >> Cells;
if (Cells.size()>0)
{
mapsize.setWidth(Cells.size());
mapsize.setHeight(Cells[0].size());
//qDebug() << "Map size" << mapsize;
//printMap();
} else qDebug("invalid size");
delete f;
delete sreader;
}
//void Map::ReadSettings()
//{
// QString str;
// SettingsReader * sreader = new SettingsReader("map.conf");
// str = sreader->getValue("cells path");
// maxPlayers = sreader->getValue("max players","1").toInt();
// QFile * f = new QFile(str);
// f->open(QIODevice::ReadOnly);
// QDataStream s(f);
// s >> Cells;
// if (Cells.size()>0)
// {
// mapsize.setWidth(Cells.size());
// mapsize.setHeight(Cells[0].size());
// //qDebug() << "Map size" << mapsize;
// //printMap();
// } else qDebug("invalid size");
// delete f;
// delete sreader;
//}
void Map::printMap()
@@ -64,8 +79,8 @@ void Map::removeAliensPath()
bool Map::addTowerOnMap(int playerId, QPoint pos)
{
if (playerId < 0 || playerId > maxPlayers) return false;
QRect r(QPoint(),mapsize);
if (playerId < 0 || playerId > players) return false;
QRect r(QPoint(),mapSize);
if (!r.contains(pos)) return false;
if (Cells[pos.x()][pos.y()]==Player+playerId)
{
@@ -95,6 +110,7 @@ bool Map::addTowerOnMap(int playerId, QPoint pos)
if (!pathOK)
{
Cells[pos.x()][pos.y()]=PlayerAlien+playerId;
emit RecreateAlienPath(pathOK);
return false;
}
return true;
@@ -104,50 +120,50 @@ bool Map::addTowerOnMap(int playerId, QPoint pos)
}
void Map::CreateMapExample()
{
Cells.resize(16);
for (int i=0; i<Cells.size(); i++)
{
Cells[i].resize(16);
Cells[i][0]=Wall;
Cells[i][Cells[i].size()-1]=Wall;
}
for (int i=0; i<Cells.size(); i++)
{
for (int j=0; j<Cells[i].size(); j++)
{
Cells[0][j]=Wall;
Cells[Cells.size()-1][j]=Wall;
}
}
for (int i=1; i<Cells.size()/2; i++)
for (int j=1; j<Cells[i].size()-1; j++)
Cells[i][j]=Player;
for (int i=Cells.size()/2; i<Cells.size()-1; i++)
for (int j=1; j<Cells[i].size()-1; j++)
Cells[i][j]=Player+1;
for (int i=2; i<Cells.size()-2; i++)
Cells[i][Cells[i].size()/2] = Wall;
Cells[Cells.size()/2][0]=Start;
Cells[Cells.size()/2-1][0]=Start;
Cells[Cells.size()/2][Cells[Cells.size()/2].size()-1]=Finish;
Cells[Cells.size()/2-1][Cells[Cells.size()/2].size()-1]=Finish;
QFile * f = new QFile("example16x16_64.map");
f->open(QIODevice::ReadWrite);
f->resize(0);
QByteArray ba;
QDataStream tmps(&ba,QIODevice::ReadWrite);
tmps << Cells;
ba = qCompress(ba);
ba = ba.toBase64();
QString str(ba);
// QDataStream s(f);
// s << ba;
f->write(str.toUtf8());
f->close();
delete f;
}
//void Map::CreateMapExample()
//{
// Cells.resize(16);
// for (int i=0; i<Cells.size(); i++)
// {
// Cells[i].resize(16);
// Cells[i][0]=Wall;
// Cells[i][Cells[i].size()-1]=Wall;
// }
// for (int i=0; i<Cells.size(); i++)
// {
// for (int j=0; j<Cells[i].size(); j++)
// {
// Cells[0][j]=Wall;
// Cells[Cells.size()-1][j]=Wall;
// }
// }
// for (int i=1; i<Cells.size()/2; i++)
// for (int j=1; j<Cells[i].size()-1; j++)
// Cells[i][j]=Player;
// for (int i=Cells.size()/2; i<Cells.size()-1; i++)
// for (int j=1; j<Cells[i].size()-1; j++)
// Cells[i][j]=Player+1;
// for (int i=2; i<Cells.size()-2; i++)
// Cells[i][Cells[i].size()/2] = Wall;
// Cells[Cells.size()/2][0]=Start;
// Cells[Cells.size()/2-1][0]=Start;
// Cells[Cells.size()/2][Cells[Cells.size()/2].size()-1]=Finish;
// Cells[Cells.size()/2-1][Cells[Cells.size()/2].size()-1]=Finish;
// QFile * f = new QFile("example16x16_64.map");
// f->open(QIODevice::ReadWrite);
// f->resize(0);
// QByteArray ba;
// QDataStream tmps(&ba,QIODevice::ReadWrite);
// tmps << Cells;
// ba = qCompress(ba);
// ba = ba.toBase64();
// QString str(ba);
//// QDataStream s(f);
//// s << ba;
// f->write(str.toUtf8());
// f->close();
// delete f;
//}
QVector<QPointF> Map::CreatePath(QPoint start, QPoint finish)
@@ -207,7 +223,7 @@ int Map::WaveTrace(QPoint start, QPoint finish)
bool stop = false;
int step = 2;
QPoint cp, tp;
QRect fr(QPoint(), mapsize);
QRect fr(QPoint(), mapSize);
QVector<QPoint> tmpp, curp;
TmpCells << Cells;
for (int i = 0; i < Cells.size(); i++)
@@ -293,7 +309,7 @@ QVector<QPoint> Map::InvWaveTrace(QPoint finish, int cnt)
chk = chk | 0x01;
c++;
}
if (wp.x() + 1 < mapsize.width() && TmpCells[wp.x()+1][wp.y()] == cnt)
if (wp.x() + 1 < mapSize.width() && TmpCells[wp.x()+1][wp.y()] == cnt)
{
chk = chk | 0x02;
c++;
@@ -303,7 +319,7 @@ QVector<QPoint> Map::InvWaveTrace(QPoint finish, int cnt)
chk = chk | 0x04;
c++;
}
if (wp.y() + 1 < mapsize.height() && TmpCells[wp.x()][wp.y()+1] == cnt)
if (wp.y() + 1 < mapSize.height() && TmpCells[wp.x()][wp.y()+1] == cnt)
{
chk = chk | 0x08;
c++;