diff options
Diffstat (limited to 'src/map.cpp')
-rw-r--r-- | src/map.cpp | 263 |
1 files changed, 133 insertions, 130 deletions
diff --git a/src/map.cpp b/src/map.cpp index 11d1d52..fa940ef 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -1,129 +1,34 @@ | |||
1 | #include "map.h" | 1 | #include "map.h" |
2 | #include "game.h" | ||
3 | #include <cstdlib> | 2 | #include <cstdlib> |
4 | #include <cstring> | 3 | #include <cstring> |
5 | #include <libxml/parser.h> | ||
6 | #include <map> | 4 | #include <map> |
7 | #include "entityfactory.h" | 5 | #include "entityfactory.h" |
8 | #include "entity.h" | 6 | #include "entity.h" |
7 | #include "game.h" | ||
8 | #include "consts.h" | ||
9 | 9 | ||
10 | static std::map<std::string, Map> maps; | 10 | Map::Map(int id) |
11 | |||
12 | Map::Map() | ||
13 | { | 11 | { |
12 | this->id = id; | ||
14 | mapdata = (int*) calloc(1, sizeof(int)); | 13 | mapdata = (int*) calloc(1, sizeof(int)); |
15 | } | 14 | } |
16 | 15 | ||
17 | Map::Map(const std::string name) | ||
18 | { | ||
19 | this->name = name; | ||
20 | |||
21 | xmlDocPtr doc = xmlParseFile(("maps/" + name + ".xml").c_str()); | ||
22 | if (doc == nullptr) | ||
23 | { | ||
24 | fprintf(stderr, "Error reading map %s\n", name.c_str()); | ||
25 | exit(-1); | ||
26 | } | ||
27 | |||
28 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
29 | if (top == nullptr) | ||
30 | { | ||
31 | fprintf(stderr, "Empty map %s\n", name.c_str()); | ||
32 | exit(-1); | ||
33 | } | ||
34 | |||
35 | if (xmlStrcmp(top->name, (const xmlChar*) "map-def")) | ||
36 | { | ||
37 | fprintf(stderr, "Invalid map definition %s\n", name.c_str()); | ||
38 | exit(-1); | ||
39 | } | ||
40 | |||
41 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
42 | { | ||
43 | if (!xmlStrcmp(node->name, (const xmlChar*) "name")) | ||
44 | { | ||
45 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
46 | if (key != 0) | ||
47 | { | ||
48 | title = (char*) key; | ||
49 | } | ||
50 | |||
51 | xmlFree(key); | ||
52 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) | ||
53 | { | ||
54 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
55 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
56 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | ||
57 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | ||
58 | { | ||
59 | mapdata[i] = atoi(strtok(NULL, ",\n")); | ||
60 | } | ||
61 | xmlFree(key); | ||
62 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities")) | ||
63 | { | ||
64 | for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) | ||
65 | { | ||
66 | if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) | ||
67 | { | ||
68 | EntityData data; | ||
69 | for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) | ||
70 | { | ||
71 | if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) | ||
72 | { | ||
73 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | ||
74 | if (key != 0) | ||
75 | { | ||
76 | data.name = (char*) key; | ||
77 | } | ||
78 | |||
79 | xmlFree(key); | ||
80 | } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) | ||
81 | { | ||
82 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | ||
83 | sscanf((char*) key, "%lf,%lf", &data.position.first, &data.position.second); | ||
84 | xmlFree(key); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | entities.push_back(data); | ||
89 | } | ||
90 | } | ||
91 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) | ||
92 | { | ||
93 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
94 | if (key != 0) | ||
95 | { | ||
96 | leftMap = &Map::getNamedMap((char*) key); | ||
97 | } | ||
98 | |||
99 | xmlFree(key); | ||
100 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) | ||
101 | { | ||
102 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
103 | if (key != 0) | ||
104 | { | ||
105 | rightMap = &Map::getNamedMap((char*) key); | ||
106 | } | ||
107 | |||
108 | xmlFree(key); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | xmlFreeDoc(doc); | ||
113 | } | ||
114 | |||
115 | Map::Map(const Map& map) | 16 | Map::Map(const Map& map) |
116 | { | 17 | { |
117 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | 18 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); |
118 | memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | 19 | memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); |
119 | 20 | ||
21 | id = map.id; | ||
120 | title = map.title; | 22 | title = map.title; |
121 | leftMap = map.leftMap; | 23 | leftMap = map.leftMap; |
122 | rightMap = map.rightMap; | 24 | rightMap = map.rightMap; |
123 | 25 | downMap = map.downMap; | |
26 | upMap = map.upMap; | ||
27 | leftType = map.leftType; | ||
28 | rightType = map.rightType; | ||
29 | upType = map.upType; | ||
30 | downType = map.downType; | ||
124 | entities = map.entities; | 31 | entities = map.entities; |
125 | |||
126 | name = map.name; | ||
127 | } | 32 | } |
128 | 33 | ||
129 | Map::Map(Map&& map) : Map() | 34 | Map::Map(Map&& map) : Map() |
@@ -149,8 +54,19 @@ void swap(Map& first, Map& second) | |||
149 | std::swap(first.title, second.title); | 54 | std::swap(first.title, second.title); |
150 | std::swap(first.leftMap, second.leftMap); | 55 | std::swap(first.leftMap, second.leftMap); |
151 | std::swap(first.rightMap, second.rightMap); | 56 | std::swap(first.rightMap, second.rightMap); |
57 | std::swap(first.downMap, second.downMap); | ||
58 | std::swap(first.upMap, second.upMap); | ||
59 | std::swap(first.leftType, second.leftType); | ||
60 | std::swap(first.rightType, second.rightType); | ||
61 | std::swap(first.upType, second.upType); | ||
62 | std::swap(first.downType, second.downType); | ||
63 | std::swap(first.id, second.id); | ||
152 | std::swap(first.entities, second.entities); | 64 | std::swap(first.entities, second.entities); |
153 | std::swap(first.name, second.name); | 65 | } |
66 | |||
67 | int Map::getID() const | ||
68 | { | ||
69 | return id; | ||
154 | } | 70 | } |
155 | 71 | ||
156 | const int* Map::getMapdata() const | 72 | const int* Map::getMapdata() const |
@@ -163,53 +79,140 @@ std::string Map::getTitle() const | |||
163 | return title; | 79 | return title; |
164 | } | 80 | } |
165 | 81 | ||
166 | const Map* Map::getLeftMap() const | 82 | void Map::createEntities(std::list<std::shared_ptr<Entity>>& entities) const |
83 | { | ||
84 | for (auto data : this->entities) | ||
85 | { | ||
86 | auto entity = EntityFactory::createNamedEntity(data.name); | ||
87 | entity->position = data.position; | ||
88 | |||
89 | entities.push_back(entity); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | bool Map::operator==(const Map& other) const | ||
94 | { | ||
95 | return id == other.id; | ||
96 | } | ||
97 | |||
98 | bool Map::operator!=(const Map& other) const | ||
99 | { | ||
100 | return id != other.id; | ||
101 | } | ||
102 | |||
103 | Map::MoveType Map::moveTypeForShort(std::string str) | ||
104 | { | ||
105 | if (str == "wrap") return MoveType::Wrap; | ||
106 | if (str == "warp") return MoveType::Warp; | ||
107 | if (str == "reverseWarp") return MoveType::ReverseWarp; | ||
108 | |||
109 | return MoveType::Wall; | ||
110 | } | ||
111 | |||
112 | Map::MoveType Map::getLeftMoveType() const | ||
113 | { | ||
114 | return leftType; | ||
115 | } | ||
116 | |||
117 | Map::MoveType Map::getRightMoveType() const | ||
118 | { | ||
119 | return rightType; | ||
120 | } | ||
121 | |||
122 | Map::MoveType Map::getUpMoveType() const | ||
123 | { | ||
124 | return upType; | ||
125 | } | ||
126 | |||
127 | Map::MoveType Map::getDownMoveType() const | ||
128 | { | ||
129 | return downType; | ||
130 | } | ||
131 | |||
132 | int Map::getLeftMapID() const | ||
167 | { | 133 | { |
168 | return leftMap; | 134 | return leftMap; |
169 | } | 135 | } |
170 | 136 | ||
171 | const Map* Map::getRightMap() const | 137 | int Map::getRightMapID() const |
172 | { | 138 | { |
173 | return rightMap; | 139 | return rightMap; |
174 | } | 140 | } |
175 | 141 | ||
176 | void Map::setLeftMap(const Map* m) | 142 | int Map::getUpMapID() const |
177 | { | 143 | { |
178 | leftMap = m; | 144 | return upMap; |
179 | } | 145 | } |
180 | 146 | ||
181 | void Map::setRightMap(const Map* m) | 147 | int Map::getDownMapID() const |
182 | { | 148 | { |
183 | rightMap = m; | 149 | return downMap; |
184 | } | 150 | } |
185 | 151 | ||
186 | void Map::createEntities(std::list<std::shared_ptr<Entity>>& entities) const | 152 | bool Map::moveTypeTakesMap(MoveType type) |
187 | { | 153 | { |
188 | for (auto data : this->entities) | 154 | switch (type) |
189 | { | 155 | { |
190 | auto entity = EntityFactory::createNamedEntity(data.name); | 156 | case MoveType::Wall: return false; |
191 | entity->position = data.position; | 157 | case MoveType::Wrap: return false; |
192 | 158 | case MoveType::Warp: return true; | |
193 | entities.push_back(entity); | 159 | case MoveType::ReverseWarp: return true; |
194 | } | 160 | } |
195 | } | 161 | } |
196 | 162 | ||
197 | bool Map::operator==(const Map& other) const | 163 | void Map::setMapdata(int* mapdata) |
198 | { | 164 | { |
199 | return name == other.name; | 165 | free(this->mapdata); |
166 | this->mapdata = mapdata; | ||
200 | } | 167 | } |
201 | 168 | ||
202 | bool Map::operator!=(const Map& other) const | 169 | void Map::setTitle(std::string title) |
203 | { | 170 | { |
204 | return name != other.name; | 171 | this->title = title; |
205 | } | 172 | } |
206 | 173 | ||
207 | Map& Map::getNamedMap(const std::string name) | 174 | void Map::setLeftMoveType(MoveType type) |
208 | { | 175 | { |
209 | if (maps.count(name) == 0) | 176 | leftType = type; |
210 | { | ||
211 | maps[name] = Map {name}; | ||
212 | } | ||
213 | |||
214 | return maps[name]; | ||
215 | } | 177 | } |
178 | |||
179 | void Map::setRightMoveType(MoveType type) | ||
180 | { | ||
181 | rightType = type; | ||
182 | } | ||
183 | |||
184 | void Map::setUpMoveType(MoveType type) | ||
185 | { | ||
186 | upType = type; | ||
187 | } | ||
188 | |||
189 | void Map::setDownMoveType(MoveType type) | ||
190 | { | ||
191 | downType = type; | ||
192 | } | ||
193 | |||
194 | void Map::setLeftMapID(int id) | ||
195 | { | ||
196 | leftMap = id; | ||
197 | } | ||
198 | |||
199 | void Map::setRightMapID(int id) | ||
200 | { | ||
201 | rightMap = id; | ||
202 | } | ||
203 | |||
204 | void Map::setUpMapID(int id) | ||
205 | { | ||
206 | upMap = id; | ||
207 | } | ||
208 | |||
209 | void Map::setDownMapID(int id) | ||
210 | { | ||
211 | downMap = id; | ||
212 | } | ||
213 | |||
214 | void Map::addEntity(EntityData& data) | ||
215 | { | ||
216 | entities.push_back(data); | ||
217 | } | ||
218 | |||