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

View File

@@ -17,31 +17,33 @@ void Aliens::AddAlien(int srcId)
qCritical("ERROR out of aliens range");
return;
}
Alien al = gameData->srcAliens.at(srcId);
AlienType al;// = gameData->srcAliens.at(srcId);
al.Id = nextId;
al.finish = gameData->map->finishs.at(qrand()%gameData->map->finishs.size());
qDebug()<<gameData->map->finishs.size()<<qrand()%gameData->map->finishs.size();
al.pos = QPointF(gameData->map->starts.at(qrand()%gameData->map->starts.size()));
al.src = srcId;
al.finish = gameData->map->Finishs().at(qrand()%gameData->map->Finishs().size());
al.pos = QPointF(gameData->map->Starts().at(qrand()%gameData->map->Starts().size()));
al.path = gameData->map->CreatePath(al.pos.toPoint(),al.finish);
al.pathIndex = 1;
if (al.path.isEmpty()) qFatal("ERROR create path");
qDebug() << al.path;
al.health = gameData->srcAliens.at(srcId).health;
al.speed = gameData->srcAliens.at(srcId).speed;
al.imgType = gameData->srcAliens.at(srcId).imgType;
//gameData->map->printMap();
gameData->curAliens.insert(al.Id,al);
nextId++;
}
void Aliens::retrace(bool *OK)
void Aliens::retrace(bool * OK)
{
qDebug("re-trace!");
gameData->map->removeAliensPath();
for (int i=0; i<gameData->curAliens.size(); i++)
{
Alien al = gameData->curAliens.take(i);
al.path = gameData->map->CreatePath(al.pos.toPoint(),al.finish);
al.pathIndex = 1;
if (al.path.isEmpty()) *OK = false;
gameData->curAliens.values()[i].path = gameData->map->CreatePath(gameData->curAliens.values()[i].pos.toPoint(),gameData->curAliens.values()[i].finish);
gameData->curAliens.values()[i].pathIndex = 1;
if (gameData->curAliens.values()[i].path.isEmpty()) *OK = false;
}
}
@@ -57,7 +59,7 @@ void Aliens::update()
QList <int> missIndex;
for (int i=0; i<gameData->curAliens.size(); i++)
{
Alien al = gameData->curAliens.values().at(i);
AlienType al = gameData->curAliens.values().at(i);
//qDebug() << i;
//if (al.health < 0) emit AlienKill();
float tmpdx,tmpdy,angl,arctg = 0;
@@ -101,7 +103,7 @@ void Aliens::update()
al.pos.setY(al.pos.y()
-al.speed*std::cos(arctg));
//return true;
gameData->curAliens[al.Id] = al;
gameData->curAliens.insert(al.Id,al);
}
for (int j=0; j<missIndex.size(); j++)
{

View File

@@ -37,7 +37,8 @@ struct srcAlienType
struct TowerType
{
int src;
int AlienId; // aim alien
int aim; // aim alien Id
int oldAim;
int PlayerId; // tower's owner
int imgType;
QPoint pos; // not QPointF because tower fixed on grid
@@ -100,7 +101,6 @@ struct SplashType
int imgType;
int src;
QPoint TowerId; // parent tower (-1;-1) for null parent
int PlayerId;
int AlienId; // aim alien (different from tower.AlienId)
QPointF destination;
int life; // in ticks
@@ -109,13 +109,13 @@ struct SplashType
};
struct Wave
struct WaveType
{
int Id;
QList <int> types; // some types of aliens (e.g. 2 fly and 3 grount in one wave)
QList <int> counts; // count aliens of each type
int wait_time; // time in ticks
unsigned int gold;
unsigned int timeout; // time in ticks
unsigned int prise;
};

View File

@@ -6,7 +6,7 @@
<alien speed="0.0099" score="350" imageType="1" prise="1" id="12" name="MONSTR" health="200" armor="1.5" regeneration="3"/>
</aliens>
<maps>
<map imageType="1" id="1" data="AAAERHicY2BgEGAA4v///0hQgmFmAGlGcjCxZuCTHzUDuxkMDOr85JrBwKC0A8kMJlwYagYOeYgZ+NXgNwMtPEbNINIMIsynOO8DAG0r1Q0=" name="Map 16x16" maxPlayers="2"/>
<map imageType="1" id="1" data="AAAERHicY2BgEGAA4v///0hQgmFmAGlGcjCxZuCTHzUDuxkMDOr85JrBwKC0A8kMJlwYagYOeYgZ+NXgNwMtPEbNINIMIsynOO8DAG0r1Q0=" name="My Map" width="16" height="16" maxPlayers="2"/>
</maps>
<waves>
<wave id="1" timeout="20000" prise="100"/>

View File

@@ -11,12 +11,11 @@ class Game_Data : public QObject
Q_OBJECT
public:
explicit Game_Data(Map * map, QObject *parent = 0);
QList <Alien> srcAliens;
QList <Tower> srcTowers;
QHash <int,Alien> curAliens;
QHash <QPoint,Tower> curTowers;
QHash <int,Splash> curSplashes;
QList <Wave> waves;
QList <srcAlienType> srcAliens;
QHash <int,AlienType> curAliens;
QHash <QPoint,TowerType> curTowers;
QHash <int,SplashType> curSplashes;
QList <WaveType> waves;
QList <Player *> players;
Map * map;
int curWave;

View File

@@ -491,13 +491,11 @@ void Loader::readMaps()
m.name = mn.namedItem("name").nodeValue();
m.imgType = mn.namedItem("imageType").nodeValue().toInt();
m.maxPlayers = mn.namedItem("maxPlayers").nodeValue().toInt();
QByteArray ba = QByteArray::fromBase64(mn.namedItem("data").nodeValue().toUtf8());
ba = qUncompress(ba);
QDataStream s(ba);
s >> m.cells;
m.data=mn.namedItem("data").nodeValue().toUtf8();
m.size.setWidth(mn.namedItem("width").nodeValue().toInt());
m.size.setHeight(mn.namedItem("height").nodeValue().toInt());
maps.insert(m.id,m);
qDebug() << tr("Map %1 id=%2 imgType=%3 maxPlayers=%5").arg(m.name).arg(m.id).arg(m.imgType).arg(m.maxPlayers);
for (int k=0; k<m.cells.size(); k++) qDebug()<<m.cells.at(k);
qDebug() << tr("Map %1 id=%2 imgType=%3 maxPlayers=%5 data=%6 ").arg(m.name).arg(m.id).arg(m.imgType).arg(m.maxPlayers).arg(QString(m.data)) << m.size;
}
qDebug("================");
qDebug() << tr("Found %1 maps").arg(maps.size());

View File

@@ -117,7 +117,7 @@ struct tbWave
int id;
unsigned int timeout;
unsigned int prise;
int type;
//int type;
};
struct tbWaveOnLevel
@@ -131,7 +131,9 @@ struct tbMap
int id;
QString name;
unsigned int maxPlayers;
QVector < QVector <int> > cells;
//QVector < QVector <int> > cells;
QByteArray data;
QSize size;
int imgType;
};
@@ -158,7 +160,7 @@ public:
explicit Loader(QObject *parent = 0);
void create();
void reload();
void loadlevel(Game_Data * gd);
Game_Data * loadlevel();
signals:
public slots:

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++;

30
map.h
View File

@@ -17,7 +17,6 @@ class Map : public QObject
{
Q_OBJECT
public:
enum CellType
{
Player = 1,
@@ -38,33 +37,38 @@ public:
// -3 - -/-
// -1000 - it is wall or some place where you can't build
QVector < QVector <int> > Cells;
QList <QPoint> starts;
QList <QPoint> finishs;
int maxPlayers;
explicit Map(int Id, QObject *parent = 0);
int Id() const {return mapId;}
explicit Map(QByteArray data, QSize size, QString name, int maxPlayers, int image, QObject *parent = 0);
bool addTowerOnMap(int playerId, QPoint pos);
bool isFreePlace(QPoint pos);
void delTowerOnMap(QPoint pos);
void removeAliensPath();
QVector <QPointF> CreatePath(QPoint start, QPoint finish);
QSize size() const {return mapsize;}
void load();
// QSize size() const {return mapsize;}
// QString name() const {return mapName;}
QList <QPoint> Starts() const {return starts;}
QList <QPoint> Finishs() const {return finishs;}
int Image() const {return imageType;}
signals:
void RecreateAlienPath(bool * pathOK);
public slots:
void printMap();
private:
int mapId;
QSize mapsize;
QSize mapSize;
QString mapName;
int players;
int imageType;
QVector < QVector <int> > Cells;
QList <QPoint> starts;
QList <QPoint> finishs;
QVector < QVector <int> > TmpCells;
QVector<QPoint> InvWaveTrace(QPoint finish, int cnt);
int WaveTrace(QPoint start, QPoint finish);
void CreateMapExample();
void ReadSettings();
//void CreateMapExample();
//void ReadSettings();
};
#endif // MAP_H

View File

@@ -4,5 +4,5 @@ Player::Player(int Id, QObject *parent) :
QObject(parent)
{
playerId = Id;
adMoney = 100;
money = 100;
}

View File

@@ -10,10 +10,10 @@ class Player : public QObject
Q_OBJECT
public:
explicit Player(int Id, QObject *parent = 0);
unsigned long int adMoney;
unsigned long int money;
unsigned long int score;
int selectAlienId;
QList <Tower> srcTowers;
QList <Splash> srcSplashes;
QList <srcTowerType> srcTowers;
int playerId;
signals:

View File

@@ -155,6 +155,8 @@
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="id" type="xsd:positiveInteger" use="required"/>
<xsd:attribute name="maxPlayers" type="xsd:positiveInteger" use="required"/>
<xsd:attribute name="width" type="xsd:positiveInteger" use="required"/>
<xsd:attribute name="height" type="xsd:positiveInteger" use="required"/>
<xsd:attribute name="imageType" type="xsd:integer" use="required"/>
<xsd:attribute name="data" type="xsd:string" use="required"/>
</xsd:complexType>

View File

@@ -11,6 +11,7 @@ Q_OBJECT
public:
explicit Splashes(Game_Data * gd, QObject *parent = 0);
void AddSplash(int srcId);
void AddSplash(srcSplashType src,TowerType tw);
void DelSplashById(int Id);
void DelSplash(int index);
void update();

View File

@@ -4,9 +4,6 @@ Towers::Towers(Game_Data *gd, QObject *parent) :
QObject(parent)
{
gameData = gd;
Tower tw;
for (int i=0; i<gameData->players.size(); i++)
gameData->players[i]->srcTowers.push_back(tw);
}
bool Towers::AddTower(int playerId, int srcId, QPoint pos)
@@ -23,7 +20,15 @@ bool Towers::AddTower(int playerId, int srcId, QPoint pos)
qCritical("ERROR out of towers range");
return false;
}
Tower tw = gameData->players.at(playerId)->srcTowers.at(srcId);
TowerType tw;
tw.src = srcId;
tw.imgType = gameData->players.at(playerId)->srcTowers.at(srcId).imgType;
tw.PlayerId = playerId;
tw.angle = 0;
tw.build = 0;
tw.reload = 0;
tw.aim = -1;
tw .oldAim = -1;
if (gameData->map->addTowerOnMap(playerId,pos))
{
tw.pos = pos;
@@ -43,5 +48,21 @@ void Towers::DelTower(QPoint pos)
void Towers::update()
{
for (int i; i<gameData->curTowers.size(); i++)
{
TowerType tw = gameData->curTowers.values().at(i);
srcTowerType stw = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src);
if (tw.build < stw.buildTime)
{
tw.build++;
} else {
// TODO: finding aim alien in this place
if (tw.reload < stw.reload)
{
tw.reload++;
} else {
emit shot(stw.splashes.at(0),tw);
}
}
}
}

View File

@@ -15,7 +15,7 @@ public:
void update();
int size() const {return gameData->curTowers.size();}
signals:
//void Shot();
void shot(srcSplashType src, TowerType tw);
public slots:
private: