forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclzones.cpp
203 lines (161 loc) · 5.13 KB
/
clzones.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "clzones.h"
#include "map.h"
#include "game.h"
#include "debug.h"
#include "output.h"
#include "mapsharing.h"
#include "translations.h"
#include "worldfactory.h"
#include "catacharset.h"
#include "ui.h"
#include <iostream>
#include <fstream>
clZones::clZones()
{
//Add new zone types here
//Use: if (g->checkZone("ZONE_NAME", posx, posy)) {
//to check for a zone
vZoneTypes.push_back(std::make_pair(_("No Auto Pickup"), "NO_AUTO_PICKUP"));
}
void clZones::clZoneData::setName()
{
const std::string sName = string_input_popup(_("Zone name:"), 55, this->sName, "", "", 15);
this->sName = (sName == "") ? _("<no name>") : sName;
}
void clZones::clZoneData::setZoneType(std::vector<std::pair<std::string, std::string> >
vZoneTypes)
{
uimenu as_m;
as_m.text = _("Select zone type:");
for (unsigned int i = 0; i < vZoneTypes.size(); ++i) {
as_m.entries.push_back(uimenu_entry(i + 1, true, (char)i + 1, vZoneTypes[i].first));
}
as_m.query();
this->sZoneType = vZoneTypes[((as_m.ret >= 1) ? as_m.ret : 1) - 1].second;
}
void clZones::clZoneData::setEnabled(const bool p_bEnabled)
{
this->bEnabled = p_bEnabled;
}
point clZones::clZoneData::getCenterPoint()
{
return point((pointStartXY.x + pointEndXY.x) / 2, (pointStartXY.y + pointEndXY.y) / 2);
}
std::string clZones::getNameFromType(const std::string p_sType)
{
for( auto &elem : vZoneTypes ) {
if( elem.second == p_sType ) {
return elem.first;
}
}
return "Unknown Type";
}
bool clZones::hasType(const std::string p_sType)
{
for( auto &elem : vZoneTypes ) {
if( elem.second == p_sType ) {
return true;
}
}
return false;
}
void clZones::cacheZoneData()
{
mZones.clear();
for( auto &elem : vZones ) {
if( elem.getEnabled() ) {
const std::string sType = elem.getZoneType();
point pStart = elem.getStartPoint();
point pEnd = elem.getEndPoint();
//draw marked area
for (int iY = pStart.y; iY <= pEnd.y; ++iY) {
for (int iX = pStart.x; iX <= pEnd.x; ++iX) {
mZones[sType].insert((iX * 100000) + iY);
}
}
}
}
}
bool clZones::hasZone(const std::string p_sType, const point p_pointInput)
{
//sure two ints as one unique int
unsigned int iTemp = (p_pointInput.x * 100000) + p_pointInput.y;
return (mZones[p_sType].find(iTemp) != mZones[p_sType].end());
}
void clZones::serialize(JsonOut &json) const
{
json.start_array();
for( auto &elem : vZones ) {
json.start_object();
json.member( "name", elem.getName() );
json.member( "type", elem.getZoneType() );
json.member( "invert", elem.getInvert() );
json.member( "enabled", elem.getEnabled() );
point pointStart = elem.getStartPoint();
point pointEnd = elem.getEndPoint();
json.member("start_x", pointStart.x);
json.member("start_y", pointStart.y);
json.member("end_x", pointEnd.x);
json.member("end_y", pointEnd.y);
json.end_object();
}
json.end_array();
}
void clZones::deserialize(JsonIn &jsin)
{
vZones.clear();
jsin.start_array();
while (!jsin.end_array()) {
JsonObject joZone = jsin.get_object();
const std::string sName = joZone.get_string("name");
const std::string sType = joZone.get_string("type");
const bool bInvert = joZone.get_bool("invert");
const bool bEnabled = joZone.get_bool("enabled");
const int iStartX = joZone.get_int("start_x");
const int iStartY = joZone.get_int("start_y");
const int iEndX = joZone.get_int("end_x");
const int iEndY = joZone.get_int("end_y");
if (hasType(sType)) {
add(sName, sType, bInvert, bEnabled, point(iStartX, iStartY), point(iEndX, iEndY));
}
}
cacheZoneData();
}
bool player::save_zones()
{
Zones.cacheZoneData();
std::string savefile = world_generator->active_world->world_path + "/" + base64_encode(
g->u.name) + ".zones.json";
try {
std::ofstream fout;
fout.exceptions(std::ios::badbit | std::ios::failbit);
fopen_exclusive(fout, savefile.c_str());
if(!fout.is_open()) {
return true; //trick game into thinking it was saved
}
fout << Zones.serialize();
fclose_exclusive(fout, savefile.c_str());
return true;
} catch(std::ios::failure &) {
popup(_("Failed to save zones to %s"), savefile.c_str());
return false;
}
}
void player::load_zones()
{
std::string savefile = world_generator->active_world->world_path + "/" + base64_encode(
g->u.name) + ".zones.json";
std::ifstream fin;
fin.open(savefile.c_str(), std::ifstream::in | std::ifstream::binary);
if(!fin.good()) {
fin.close();
return;
}
try {
JsonIn jsin(fin);
Zones.deserialize(jsin);
} catch (std::string e) {
DebugLog(D_ERROR, DC_ALL) << "load_zones: " << e;
}
fin.close();
}