-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terrain.hpp
76 lines (61 loc) · 2.92 KB
/
Terrain.hpp
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
#include <glm/vec2.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext.hpp>
#include <vector>
#include "BinaryReader.h"
int load_terrain(std::ostream* out, std::string& input, bool debug)
{
std::ifstream fin(input, std::ios_base::binary);
fin.seekg(0, std::ios::end);
const size_t fileSize = fin.tellg();
fin.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(fileSize);
fin.read(reinterpret_cast<char*>(buffer.data()), fileSize);
fin.close();
BinaryReader reader(buffer);
const std::string magic_number = reader.read_string(4);
if (magic_number != "W3E!") {
std::cerr << "Invalid war3map.w3e file: Magic number is not W3E!";
return 13;
}
// Version
*out << "Version: " << reader.read<uint32_t>() << '\n'
<< "Tileset: " << reader.read<char>() << '\n'
<< "Custom Tileset: " << std::boolalpha << (bool)reader.read<uint32_t>() << '\n';
const uint32_t tileset_textures = reader.read<uint32_t>();
*out << "Tileset textures: ";
for (size_t i = 0; i < tileset_textures; i++) {
*out << reader.read_string(4) << ' ';
}
const int cliffset_textures = reader.read<uint32_t>();
*out << "\nCliffset textures: ";
for (int i = 0; i < cliffset_textures; i++) {
*out << reader.read_string(4) << ' ';
}
uint32_t width = reader.read<uint32_t>();
uint32_t height = reader.read<uint32_t>();
*out << "\nWidth: " << width << '\n'
<< "Height: " << height << '\n'
<< "Offset: " << glm::to_string(reader.read<glm::vec2>()) << "\n\n";
if (debug) //Terrain changes are not readable
for (uint32_t j = 0; j < height; j++)
for (uint32_t i = 0; i < width; i++) {
*out << "Height: " << reader.read<uint16_t>() << '\n';
const uint16_t water_and_edge = reader.read<uint16_t>();
*out << "Water height: " << (water_and_edge & 0x3FFF) << '\n'
<< "Is map edge: " << (bool)(water_and_edge & 0x4000) << '\n';
const uint8_t texture_and_flags = reader.read<uint8_t>();
*out << "ground texture: " << (texture_and_flags & 0b00001111) << '\n'
<< "Has ramp: " << (bool)(texture_and_flags & 0b00010000) << '\n'
<< "Has blight: " << (bool)(texture_and_flags & 0b00100000) << '\n'
<< "Has water: " << (bool)(texture_and_flags & 0b01000000) << '\n'
<< "Has boundary: " << (bool)(texture_and_flags & 0b10000000) << '\n';
const uint8_t variation = reader.read<uint8_t>();
*out << "Ground variation: " << (variation & 0b00011111) << '\n'
<< "Cliff variation: " << ((variation & 0b11100000) >> 5) << '\n';
const uint8_t misc = reader.read<uint8_t>();
*out << "Cliff texture: " << ((misc & 0b11110000) >> 4) << '\n'
<< "Layer height: " << (misc & 0b00001111) << "\n\n";
}
return 0;
}