forked from damar1st/TSTO-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildinginfo.cpp
161 lines (142 loc) · 6.2 KB
/
buildinginfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ///////////////////////////////////////////////////////////////////////// //
// //
// Copyright (C) 2016 by Oleg Polivets //
// [email protected] //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// ///////////////////////////////////////////////////////////////////////// //
#include "buildinginfo.h"
#include <QXmlStreamReader>
#include <QImage>
#include <QDebug>
// set XML description
void BuildingInfo::setXml(const QString & xml) {
mXml = xml;
QXmlStreamReader reader(xml);
for (;!reader.atEnd(); reader.readNext()) {
if (!reader.isStartElement()) continue;
const QXmlStreamAttributes & sa = reader.attributes();
if (reader.name().compare("Unique") == 0) {
mUnique = sa[0].value().toString().compare("true") == 0;
} else if (reader.name().compare("Group") == 0) {
mGroups.push_back( sa[0].value().toString() );
} else if (reader.name().compare("GridType") == 0) {
setGridType(sa[0].value().toString());
} else if (reader.name().compare("Bonus") == 0) {
for (int i = 0; i < sa.count(); ++i) {
mBonus[ sa[i].name().toString() ]
= sa[i].value().toString().toFloat();
}
} else if (reader.name().compare("RatingElem") == 0) {
for (int i = 0; i < sa.count(); ++i) {
mRatings[ sa[i].name().toString() ]
= sa[i].value().toString().toInt();
}
} else if (reader.name().compare("Character") == 0) {
mCharacterOrJob = true;
} else if (reader.name().compare("Job") == 0) {
mCharacterOrJob = true;
} else if (reader.name().compare("Cost") == 0) {
for (int i = 0; i < sa.count(); ++i) {
if (sa[i].name().toString().compare("donuts") == 0 ) {
mCostDonuts = sa[i].value().toString().toInt();
}
}
} else if (reader.name().compare("BSv2") == 0) {
fetchBSv2Tag(reader);
}
}
}
void BuildingInfo::setGridType(const QString & gt) {
/*
<GridTypeMap base_type="grass">
<GridTypeRange type="ocean" x_end="-3" />
<GridTypeRange type="shoreline" x_start="-3" x_end="0" />
<GridTypeRange type="beach" x_start="0" x_end="4" />
</GridTypeMap>
*/
int r = 0;
static QStringList gridString = QStringList()
<< "grass" << "pavement" << "sidewalk" << "road" << "wall"
<< "river" << "shoreline"
<< "ocean" << "beach" << "boardwalk" << "boardwalkedge"
<< "klroad" << "klroadedge" << "klparking";
mGrids = gt.split('|');
foreach(const QString & k, mGrids) {
const int idx = gridString.indexOf(k);
if (idx > -1) r |= (1 << idx); // else qDebug() << "unknown gridType =" << k;
}
mGridType = r;
}
// parse XML data with building info
bool BuildingInfo::fetchAllBuildInfo(const QString & fn, QIODevice * f) {
if (!f || !f->open(QIODevice::ReadOnly)) {
qDebug() << "BuildingInfo::fetchAllBuildInfo(): can't open file" << fn;
return false;
}
QXmlStreamReader reader(f->readAll());
f->close();
bool ok = false;
for (;!reader.atEnd(); reader.readNext()) {
if (reader.isStartElement() && reader.name() == "Building") {
const QXmlStreamAttributes & sa = reader.attributes();
if (sa.count() < 6) continue;
this->x = sa[0].value().toString().toFloat();
this->z = sa[1].value().toString().toFloat();
this->height = sa[2].value().toString().toFloat();
this->locX = sa[3].value().toString().toFloat();
this->locY = sa[4].value().toString().toFloat();
this->transImageX = sa[5].value().toString().toFloat();
this->transImageY = sa[6].value().toString().toFloat();
this->mFetched = true;
#if (0)
qDebug() << this->x << this->z
<< this->height
<< this->locX << this->locY
<< this->transImageX << this->transImageY;
#endif
ok = true;
break;
}
}
if (!ok) {
qDebug() << "can't fetchAllBuildInfo() for " << fn;
}
return ok;
}
// fetch BSv2 tag of xml
void BuildingInfo::fetchBSv2Tag(QXmlStreamReader & reader) {
const QXmlStreamAttributes & ss = reader.attributes();
// fill images names for each flip direction
mImgNames.clear();
if (ss.count() >= 4) {
Q_ASSERT(ss.count() >= 4);
mDirectionsToFlip = 4;
mImgNames.push_back(ss[0].value().toString().toLower());
mImgNames.push_back(ss[1].value().toString().toLower());
mImgNames.push_back(ss[3].value().toString().toLower());
mImgNames.push_back(ss[2].value().toString().toLower());
} else {
mDirectionsToFlip = 2;
mImgNames.push_back(ss[0].value().toString().toLower());
mImgNames.push_back(
(ss.count() > 1 && ss[1].name() == "flippedname")
? ss[1].value().toString().toLower()
: QString() );
}
// rgb image resources associated with each flip direction
qDeleteAll(mImgs);
mImgs.clear();
for (int i = 0; i < mDirectionsToFlip; ++i) {
mImgs.push_back(0);
}
}