summary refs log tree commit diff stats
path: root/tools/mapedit/src/map.cpp
blob: 2799a4ee757c7f6b2b997a4f1b519763f88579c6 (plain) (blame)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "map.h"
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include <sstream>

Map::Map()
{
  mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int));
}

Map::Map(std::string filename)
{
  xmlDocPtr doc = xmlParseFile(filename.c_str());
  if (doc == nullptr)
  {
    throw MapLoadException(filename);
  }
  
  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr)
  {
    throw MapLoadException(filename);
  }
  
  if (xmlStrcmp(top->name, (const xmlChar*) "map-def"))
  {
    throw MapLoadException(filename);
  }
  
  for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
  {
    if (!xmlStrcmp(node->name, (const xmlChar*) "name"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      title = (char*) key;
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
      mapdata[0] = atoi(strtok((char*) key, ",\n"));
      for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
      {
        mapdata[i] = atoi(strtok(NULL, ",\n"));
      }
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      leftmap = (char*) key;
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      rightmap = (char*) key;
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities"))
    {
      for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next)
      {
        if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity"))
        {
          auto data = std::make_shared<MapObjectEntry>();
          
          for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next)
          {
            if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type"))
            {
              xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1);
              data->object = MapObject::getAllObjects().at((char*) key).get();
              xmlFree(key);
            } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position"))
            {
              xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1);
              sscanf((char*) key, "%lf,%lf", &data->position.first, &data->position.second);
              xmlFree(key);
            }
          }
        
          objects.push_back(data);
        }
      }
    }
  }
  
  xmlFreeDoc(doc);
}

Map::Map(const Map& map)
{
  mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
  memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int));
  
  title = map.title;
  leftmap = map.leftmap;
  rightmap = map.rightmap;
  dirty = map.dirty;
  objects = map.objects;
}

Map::Map(Map&& map) : Map()
{
  swap(*this, map);
}

Map::~Map()
{
  free(mapdata);
}

Map& Map::operator= (Map map)
{
  swap(*this, map);
  
  return *this;
}

void swap(Map& first, Map& second)
{
  std::swap(first.mapdata, second.mapdata);
  std::swap(first.title, second.title);
  std::swap(first.leftmap, second.leftmap);
  std::swap(first.rightmap, second.rightmap);
  std::swap(first.dirty, second.dirty);
  std::swap(first.objects, second.objects);
}

#define MY_ENCODING "ISO-8859-1"

void Map::save(std::string name)
{
  if (!dirty) return;
  
  int rc;
  
  xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0);
  if (writer == NULL) throw MapWriteException(name);

  rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterStartElement(writer, (xmlChar*) "map-def");
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) title.c_str());
  if (rc < 0) throw MapWriteException(name);
  
  std::ostringstream mapdata_out;
  for (int y=0; y<MAP_HEIGHT; y++)
  {
    for (int x=0; x<MAP_WIDTH; x++)
    {
      mapdata_out << mapdata[x+y*MAP_WIDTH] << ",";
    }
    
    mapdata_out << std::endl;
  }
  
  rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str());
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str());
  if (rc < 0) throw MapWriteException(name);

  rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str());
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterStartElement(writer, (xmlChar*) "entities");
  if (rc < 0) throw MapWriteException(name);
  
  for (auto object : objects)
  {
    rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity");
    if (rc < 0) throw MapWriteException(name);
    
    rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object->object->getType().c_str());
    if (rc < 0) throw MapWriteException(name);
    
    std::ostringstream entpos_out;
    entpos_out << object->position.first << "," << object->position.second;
    
    rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str());
    if (rc < 0) throw MapWriteException(name);
    
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) throw MapWriteException(name);
  }
  
  rc = xmlTextWriterEndElement(writer);
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterEndElement(writer);
  if (rc < 0) throw MapWriteException(name);
  
  rc = xmlTextWriterEndDocument(writer);
  if (rc < 0) throw MapWriteException(name);
  
  xmlFreeTextWriter(writer);
  
  dirty = false;
}

bool Map::hasUnsavedChanges() const
{
  return dirty;
}

void Map::setTileAt(int x, int y, int tile)
{
  dirty = true;
  mapdata[x+y*MAP_WIDTH] = tile;
}

int Map::getTileAt(int x, int y) const
{
  return mapdata[x+y*MAP_WIDTH];
}

std::string Map::getTitle() const
{
  return title;
}

void Map::setTitle(std::string title)
{
  dirty = true;
  this->title = title;
}

const std::list<std::shared_ptr<MapObjectEntry>>& Map::getObjects() const
{
  return objects;
}

void Map::addObject(std::shared_ptr<MapObjectEntry>& obj)
{
  dirty = true;
  objects.push_back(obj);
}

void Map::removeObject(std::shared_ptr<MapObjectEntry>& obj)
{
  dirty = true;
  objects.remove(obj);
}