#include "loader.h" Loader::Loader() { doc = new QDomDocument("data"); qDebug() << createMapExample(); } void Loader::load(QString filename) { validate(filename); file = new QFile(filename); if (!file->open(QIODevice::ReadOnly)) qFatal("No file data.xml found!"); if (!doc->setContent(file)) { file->close(); qFatal("Error parsing data.xml"); } readAnimations(); readAliens(); readMaps(); readLevels(); readSplashes(); readTowers(); readTriggers(); readChains(); //readUpgrades(); readLevTowers(); readMorphs(); readWaves(); readWaveParts(); readLevWaves(); file->close(); } void Loader::save(QString filename) { QDomDocument data("data"); QFile cfile(filename); if (!cfile.open(QIODevice::WriteOnly)) return; cfile.resize(0); QDomElement elem; QDomElement root = data.createElement("ADdata"); data.appendChild(root); QDomElement child = data.createElement("aliens"); root.appendChild(child); for (int i=0; i 0) elem.setAttribute("armor",al.armor); if (al.isFlying) elem.setAttribute("flying","true"); elem.setAttribute("prise",al.prise); if (al.regeneration > 0) elem.setAttribute("regeneration",al.regeneration); elem.setAttribute("score",al.score); elem.setAttribute("speed",al.speed); elem.setAttribute("imageType",al.imgType); } child = data.createElement("maps"); root.appendChild(child); for (int i=0; i 0) elem.setAttribute("expByDamage",tw.expByDamage); if (tw.expByKill > 0) elem.setAttribute("expByKill",tw.expByKill); if (tw.expByShot > 0) elem.setAttribute("expByShot",tw.expByShot); } child = data.createElement("triggers"); root.appendChild(child); for (int i=0; i 0) elem.setAttribute("count",trig.count); if (trig.damage > 0) elem.setAttribute("damage",trig.damage); if (trig.radius > 0) elem.setAttribute("radius",trig.radius); if (trig.delParent) elem.setAttribute("delParent","true"); if (trig.randomPosRadius > 0) elem.setAttribute("randomPosRadius",trig.randomPosRadius); switch (trig.childAim) { case srcTriggerType::allSide : elem.setAttribute("childAim","allSide"); break; case srcTriggerType::nearestAlien : elem.setAttribute("childAim","nearestAlien"); break; case srcTriggerType::parentAim : elem.setAttribute("childAim","parentAim"); break; default: elem.setAttribute("childAim","noAim"); break; } switch (trig.type) { case srcTriggerType::onTimer : elem.setAttribute("type","onTimer"); elem.setAttribute("timer",trig.timer); break; case srcTriggerType::onDestination : elem.setAttribute("type","onDestination"); break; case srcTriggerType::onAlienInRadius : elem.setAttribute("type","onAlienInRadius"); break; } } child = data.createElement("chains"); root.appendChild(child); for (int i=0; i 0) elem.setAttribute("child",ch.childSplashId); } child = data.createElement("towersOnLevels"); root.appendChild(child); for (int i=0; imaxPlayers(); i++) { Player * pl = new Player(i); gd->players.append(pl); } qDebug() << gd->players.size(); for (int i=0; imaxPlayers(); ++i) gd->players.push_back(new Player(i)); QList wollist = levWaves.values(id); if (wollist.isEmpty()) { qCritical() << tr("Error no waves on level!"); return 0; } tbWave tbw; tbWavePart tbwpt; QList wplist; QList alienIds; QList animIds; for (int i=0; iwaves.prepend(wt); qDebug() << tr("load wave #%1 prise=%2 timeout=%3").arg(i+1).arg(wt.prise).arg(wt.timeout) << wt.types << wt.counts; wplist.clear(); } wollist.clear(); for (int i=0; ialiens->srcAliens.append(salt); qDebug() << tr("Alien %1 #%2 ").arg(salt.name).arg(i); } QList tollist = levTowers.values(id); tbTower tbt; QList < QList > morph_indexes; for (int i=0; i chlist = chains.values(tbt.id); QList trigIds; tbSplash tbs = splashes.value(tbt.splashId); srcSplashType sst; sst.autoControl = tbs.autoControl; if (!animIds.contains(tbs.imgType)) animIds.append(tbs.imgType); sst.imgType = animIds.indexOf(tbs.imgType); sst.lifetime = tbs.lifetime; sst.name = tbs.name; sst.speed = tbs.speed; sst.animSpeed = tbs.animSpeed; sst.ZValue = tbs.ZValue; sst.compositionMode = tbs.compositionMode; sst.keepOnAlien = tbs.keepOnAlien; stt.splashes.append(sst); int recursiveCheck = 1; fillTowerChains(&animIds,&chlist,&trigIds,&stt, tbt.splashId,0,&recursiveCheck); morph_indexes.append(QList()); QList morphlist = morphs.values(tbt.id); bool contain; tbTowerOnLevel j; MorphType m; foreach (tbMorph tmh, morphlist) { contain = false; foreach(j,tollist) { if (j.towerId == tmh.toId) { contain = true; break; } } m.cost = tmh.cost; m.expRequired = tmh.expRequired; m.morph = (srcTowerType*)tollist.indexOf(j); if (contain) morph_indexes[i].append(m); } for (int k=0; kmaxPlayers(); ++k) gd->players[k]->srcTowers.append(stt); qDebug() << tr("Tower %1 trig=%2 splash=%3").arg(stt.name).arg(stt.triggers.size()).arg(stt.splashes.size()) << tr("chain lenght %1").arg(recursiveCheck); } for (int i=0; imaxPlayers(); ++k) { j.morph = &(gd->players[k]->srcTowers[index]); gd->players[k]->srcTowers[i].morphs.append(j); } } } QString morphtree; foreach (srcTowerType i, gd->players[0]->srcTowers) { printMorphs(&i,morphtree,""); } qDebug() << morphtree + "\n"; /// =================================== for (int i=0; i; for (int k=0; kappend(new QImage(animations.value(animIds.at(i)) .pathes.at(k))); gd->curAnimations.append(anim); } for (int i=0; ialiens->srcAliens.size(); i++) gd->aliens->srcAliens[i].images = gd->curAnimations.at(gd->aliens->srcAliens.at(i).imgType).images; for (int i=0; iplayers.size(); i++) for (int j=0; jplayers.at(i)->srcTowers.size(); j++) { gd->players[i]->srcTowers[j].image = gd->curAnimations.at(gd->players.at(i)->srcTowers.at(j).imgType).images->at(0); for (int k=0; kplayers.at(i)->srcTowers.at(j).splashes.size(); k++) gd->players[i]->srcTowers[j].splashes[k].images = gd->curAnimations.at(gd->players.at(i)->srcTowers.at(j).splashes.at(k).imgType).images; } gd->towerSelect.images = gd->alienSelect.images = 0; gd->blood = 0; for (QHash::iterator i = animations.begin(); i != animations.end(); ++i) { if (i->name == "tower select") { AnimationType anim; anim.images = new QList; for (int k = 0; k < i->pathes.size(); k++) anim.images->append(new QImage(i->pathes.at(k))); gd->towerSelect = anim; } if (i->name == "alien select") { AnimationType anim; anim.images = new QList; for (int k = 0; k < i->pathes.size(); k++) anim.images->append(new QImage(i->pathes.at(k))); gd->alienSelect = anim; } if (i->name == "blood") { gd->blood = new QImage(i->pathes.at(0)); QSize size = gd->blood->size(); qDebug() << size; gd->bloodrect = QRectF(QPointF(-(size.width()/2),-(size.height()/2)),size); } } if (gd->blood == 0) gd->blood = new QImage(); if (gd->towerSelect.images == 0) { AnimationType anim; anim.images = new QList; anim.images->append(new QImage(":/images/select.png")); gd->towerSelect = anim; } if (gd->alienSelect.images == 0) { AnimationType anim; anim.images = new QList; anim.images->append(new QImage(":/images/select.png")); gd->alienSelect = anim; } return gd; } void Loader::fillTowerChains(QList * animIds, QList * chlist, QList * trigIds, srcTowerType * stt, int parId, int parIndex, int * recursiveCheck) { if (*recursiveCheck > 100) { qCritical("circle dependencies detected!"); return; } for (int i=0; isize(); i++) { if (chlist->at(i).parentSplashId == parId) { int trigid = chlist->at(i).triggerId; if (!(trigIds->contains(trigid))) { srcTriggerType strigt; tbTrigger tbtrig = triggers.value(trigid); strigt.count = tbtrig.count; strigt.damage = tbtrig.damage; strigt.delParent = tbtrig.delParent; strigt.radius = tbtrig.radius; if (strigt.radius <= 0 && strigt.damage>0 && !splashes.value(parId).autoControl) qCritical("Wrong trigger or chain, because damage >0 radius=0 and not autocontrol.\nThis damage will be ignored!"); strigt.randomPosRadius = tbtrig.randomPosRadius; strigt.timer = tbtrig.timer; strigt.type = tbtrig.type; strigt.childAim = tbtrig.childAim; strigt.childId = -1; stt->triggers.append(strigt); trigIds->append(trigid); } stt->splashes[parIndex].triggerIndexes.append(trigIds->indexOf(trigid)); if (chlist->at(i).childSplashId > 0 && stt->triggers.at(trigIds->indexOf(trigid)).count > 0) { tbSplash tbs = splashes.value(chlist->at(i).childSplashId); srcSplashType sst; sst.autoControl = tbs.autoControl; if (!animIds->contains(tbs.imgType)) animIds->append(tbs.imgType); sst.imgType = animIds->indexOf(tbs.imgType); sst.lifetime = tbs.lifetime; sst.name = tbs.name; sst.speed = tbs.speed; sst.animSpeed = tbs.animSpeed; sst.ZValue = tbs.ZValue; sst.compositionMode = tbs.compositionMode; sst.keepOnAlien = tbs.keepOnAlien; stt->splashes.append(sst); stt->triggers[stt->splashes.at(parIndex).triggerIndexes.size()-1].childId = stt->splashes.size() - 1; *recursiveCheck += 1; fillTowerChains(animIds,chlist,trigIds,stt,chlist->at(i).childSplashId, stt->splashes.size()-1,recursiveCheck); } } } } QString Loader::createMapExample() { QVector < QVector < int > > Cells; Cells.resize(25); for (int i=0; ielementsByTagName("alien"); for (int i=0; ielementsByTagName("wave"); for (int i=0; ielementsByTagName("wavePart"); for (int i=0; ielementsByTagName("chain"); for (int i=0; i 0) { if (!splashes.contains(chain.childSplashId)) { qCritical("invalid child splash Id"); OK = false; } } chain.parentSplashId = nm.namedItem("parent").nodeValue().toInt(); if (!splashes.contains(chain.parentSplashId)) { qCritical("invalid parent splash Id"); OK = false; } chain.triggerId = nm.namedItem("trigger").nodeValue().toInt(); if (!triggers.contains(chain.triggerId)) { qCritical("invalid parent splash Id"); OK = false; } if (OK) { chains.insert(chain.towerId,chain); //qDebug() << tr("Tower=%1 parent=%2 child=%3 trigger=%4").arg(chain.towerId).arg(chain.parentSplashId).arg(chain.childSplashId).arg(chain.triggerId); } } //qDebug("================"); qDebug() << tr("Found %1 chains").arg(chains.size()); //qDebug("================"); } void Loader::readMorphs() { morphs.clear(); QDomNodeList celems = doc->elementsByTagName("morph"); for (int i=0; ielementsByTagName("waveOnLevel"); for (int i=0; ielementsByTagName("towerOnLevel"); for (int i=0; ielementsByTagName("level"); for (int i=0; ielementsByTagName("trigger"); for (int i=0; i0)) trig.chance = 100; str = nm.namedItem("delParent").nodeValue(); trig.delParent = (str == "true"); str = nm.namedItem("type").nodeValue(); if (str == "onDestination") trig.type = srcTriggerType::onDestination; if (str == "onTimer") trig.type = srcTriggerType::onTimer; if (str == "onAlienInRadius") trig.type = srcTriggerType::onAlienInRadius; str = nm.namedItem("childAim").nodeValue(); trig.childAim = srcTriggerType::noAim; if (str == "allSide") trig.childAim = srcTriggerType::allSide; if (str == "parentAim") trig.childAim = srcTriggerType::parentAim; if (str == "nearestAlien") trig.childAim = srcTriggerType::nearestAlien; triggers.insert(trig.id,trig); //qDebug() << tr("Trigger %1 id=%2 damage=%3 radius=%4 delParent=%5 timer=%6").arg(trig.name).arg(trig.id).arg(trig.damage).arg(trig.radius).arg(trig.delParent).arg(trig.timer) // << tr("count=%1 chance=%2 randomPosRadius=%3 type=%4 childAim=%5").arg(trig.count).arg(trig.chance).arg(trig.randomPosRadius).arg(trig.type).arg(trig.childAim); } //qDebug("================"); qDebug() << tr("Found %1 triggers").arg(triggers.size()); //qDebug("================"); } void Loader::readSplashes() { splashes.clear(); QString str; QDomNodeList selems = doc->elementsByTagName("splash"); for (int i=0; ielementsByTagName("animation"); for (int i=0; ielementsByTagName("tower"); for (int i=0; ielementsByTagName("map"); for (int i=0; iname + ":"; foreach (MorphType i, stt->morphs) printMorphs(i.morph,str,prefix+" "); }