1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_IOMAP_H_8085D4B1037A44288494A52FDBB775E4
21#define FS_IOMAP_H_8085D4B1037A44288494A52FDBB775E4
22
23#include "item.h"
24#include "map.h"
25#include "house.h"
26#include "spawn.h"
27#include "configmanager.h"
28
29extern ConfigManager g_config;
30
31enum OTBM_AttrTypes_t {
32 OTBM_ATTR_DESCRIPTION = 1,
33 OTBM_ATTR_EXT_FILE = 2,
34 OTBM_ATTR_TILE_FLAGS = 3,
35 OTBM_ATTR_ACTION_ID = 4,
36 OTBM_ATTR_UNIQUE_ID = 5,
37 OTBM_ATTR_TEXT = 6,
38 OTBM_ATTR_DESC = 7,
39 OTBM_ATTR_TELE_DEST = 8,
40 OTBM_ATTR_ITEM = 9,
41 OTBM_ATTR_DEPOT_ID = 10,
42 OTBM_ATTR_EXT_SPAWN_FILE = 11,
43 OTBM_ATTR_RUNE_CHARGES = 12,
44 OTBM_ATTR_EXT_HOUSE_FILE = 13,
45 OTBM_ATTR_HOUSEDOORID = 14,
46 OTBM_ATTR_COUNT = 15,
47 OTBM_ATTR_DURATION = 16,
48 OTBM_ATTR_DECAYING_STATE = 17,
49 OTBM_ATTR_WRITTENDATE = 18,
50 OTBM_ATTR_WRITTENBY = 19,
51 OTBM_ATTR_SLEEPERGUID = 20,
52 OTBM_ATTR_SLEEPSTART = 21,
53 OTBM_ATTR_CHARGES = 22,
54};
55
56enum OTBM_NodeTypes_t {
57 OTBM_ROOTV1 = 1,
58 OTBM_MAP_DATA = 2,
59 OTBM_ITEM_DEF = 3,
60 OTBM_TILE_AREA = 4,
61 OTBM_TILE = 5,
62 OTBM_ITEM = 6,
63 OTBM_TILE_SQUARE = 7,
64 OTBM_TILE_REF = 8,
65 OTBM_SPAWNS = 9,
66 OTBM_SPAWN_AREA = 10,
67 OTBM_MONSTER = 11,
68 OTBM_TOWNS = 12,
69 OTBM_TOWN = 13,
70 OTBM_HOUSETILE = 14,
71 OTBM_WAYPOINTS = 15,
72 OTBM_WAYPOINT = 16,
73};
74
75enum OTBM_TileFlag_t : uint32_t {
76 OTBM_TILEFLAG_PROTECTIONZONE = 1 << 0,
77 OTBM_TILEFLAG_NOPVPZONE = 1 << 2,
78 OTBM_TILEFLAG_NOLOGOUT = 1 << 3,
79 OTBM_TILEFLAG_PVPZONE = 1 << 4
80};
81
82#pragma pack(1)
83
84struct OTBM_root_header {
85 uint32_t version;
86 uint16_t width;
87 uint16_t height;
88 uint32_t majorVersionItems;
89 uint32_t minorVersionItems;
90};
91
92struct OTBM_Destination_coords {
93 uint16_t x;
94 uint16_t y;
95 uint8_t z;
96};
97
98struct OTBM_Tile_coords {
99 uint8_t x;
100 uint8_t y;
101};
102
103#pragma pack()
104
105class IOMap
106{
107 static Tile* createTile(Item*& ground, Item* item, uint16_t x, uint16_t y, uint8_t z);
108
109 public:
110 bool loadMap(Map* map, const std::string& fileName);
111
112 /* Load the spawns
113 * \param map pointer to the Map class
114 * \returns Returns true if the spawns were loaded successfully
115 */
116 static bool loadSpawns(Map* map) {
117 if (map->spawnfile.empty()) {
118 //OTBM file doesn't tell us about the spawnfile,
119 //lets guess it is mapname-spawn.xml.
120 map->spawnfile = g_config.getString(ConfigManager::MAP_NAME);
121 map->spawnfile += "-spawn.xml";
122 }
123
124 return map->spawns.loadFromXml(map->spawnfile);
125 }
126
127 /* Load the houses (not house tile-data)
128 * \param map pointer to the Map class
129 * \returns Returns true if the houses were loaded successfully
130 */
131 static bool loadHouses(Map* map) {
132 if (map->housefile.empty()) {
133 //OTBM file doesn't tell us about the housefile,
134 //lets guess it is mapname-house.xml.
135 map->housefile = g_config.getString(ConfigManager::MAP_NAME);
136 map->housefile += "-house.xml";
137 }
138
139 return map->houses.loadHousesXML(map->housefile);
140 }
141
142 const std::string& getLastErrorString() const {
143 return errorString;
144 }
145
146 void setLastErrorString(std::string error) {
147 errorString = error;
148 }
149
150 private:
151 bool parseMapDataAttributes(OTB::Loader& loader, const OTB::Node& mapNode, Map& map, const std::string& fileName);
152 bool parseWaypoints(OTB::Loader& loader, const OTB::Node& waypointsNode, Map& map);
153 bool parseTowns(OTB::Loader& loader, const OTB::Node& townsNode, Map& map);
154 bool parseTileArea(OTB::Loader& loader, const OTB::Node& tileAreaNode, Map& map);
155 std::string errorString;
156};
157
158#endif
159