-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldMap.cpp
203 lines (165 loc) · 6.27 KB
/
WorldMap.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
// This file is part of the WinRayCast Application (a 3D Engine Demo).
// Copyright (C) 2005 - 2018
// Antonino Calderone (antonino.calderone@gmail.com)
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
/* -------------------------------------------------------------------------- */
#include "WorldMap.h"
#include <fstream>
#include "./miptknzr/include/mip_unicode.h"
#include "./miptknzr/include/mip_tknzr_bldr.h"
#include "./miptknzr/include/mip_esc_cnvrtr.h"
#include <iostream>
/* -------------------------------------------------------------------------- */
bool WorldMap::setMapInfo(const Cell* array, uint32_t rows, uint32_t cols)
{
if (rows <= 0 || cols <= 0) {
return false;
}
m_map.resize(rows);
int i = 0;
for (uint32_t r = 0; r < rows; ++r) {
m_map[r].resize(cols);
for (uint32_t c = 0; c < cols; ++c) {
m_map[r][c] = array[i++];
}
}
m_maxX = getCellDx() * getColCount();
m_maxY = getCellDy() * getRowCount();
return true; // success
}
/* -------------------------------------------------------------------------- */
bool WorldMap::load(const std::string& fileName)
{
mip::tknzr_bldr_t bldr;
bldr.def_atom(_T("{"));
bldr.def_atom(_T("}"));
bldr.def_atom(_T(","));
bldr.def_sl_comment(_T("//"));
bldr.def_sl_comment(_T("#"));
bldr.def_blank(_T(" "));
bldr.def_blank(_T("\r")); // treat \r like a blank
bldr.def_blank(_T("\t"));
bldr.def_eol(mip::base_tknzr_t::eol_t::LF); // linefeed is end of line marker
bldr.def_string(_T('\"'), std::make_shared<mip::esc_cnvrtr_t>(_T('\\')));
bldr.def_ml_comment(_T("/*"), _T("*/"));
auto tknzr = bldr.build();
mip::_ifstream is(fileName, std::ios::in | std::ios::binary);
if (!is.is_open()) {
return false;
}
enum state_t {
ANY_KEY,
MAP_BEGIN,
MAP_VAL,
TEXTURE_BEGIN,
TEXTURE_KEY,
TEXTURE_VALUE
};
state_t st = ANY_KEY;
std::vector<uint64_t> mapValues;
std::map<std::string, std::string> textureMap;
std::string txtKey;
int cols = -1;
int rows = 0;
int offset = 0;
using tcl_t = mip::token_t::tcl_t;
while (is.is_open() && !is.bad()) {
auto tkn = tknzr->next(is);
if (!tkn) {
return false;
}
switch (tkn->type()) {
case mip::token_t::tcl_t::END_OF_FILE: {
setMapInfo(mapValues.data(), rows, cols);
}
return true;
case mip::token_t::tcl_t::BLANK:
case mip::token_t::tcl_t::COMMENT:
case mip::token_t::tcl_t::END_OF_LINE:
break;
case mip::token_t::tcl_t::ATOM:
case mip::token_t::tcl_t::STRING:
case mip::token_t::tcl_t::OTHER: {
switch (st) {
case MAP_BEGIN:
if (tkn->type() == tcl_t::ATOM && tkn->value() == "{") {
st = MAP_VAL;
break;
}
return false;
case ANY_KEY:
if (tkn->type()==tcl_t::OTHER && tkn->value() == "map") {
st = MAP_BEGIN;
break;
}
else if (tkn->type() == tcl_t::OTHER && tkn->value() == "tmap") {
st = TEXTURE_BEGIN;
break;
}
return false;
case MAP_VAL:
if (tkn->type() == tcl_t::ATOM) {
if (tkn->value() == ",") {
if (cols < 0) {
cols = offset;
}
else {
if (cols != offset) {
return false;
}
}
offset = 0;
++rows;
}
else if (tkn->value() == "}") {
st = ANY_KEY;
++rows;
break;
}
}
else if (tkn->type() == tcl_t::OTHER) {
try {
mapValues.push_back(
std::stoll(tkn->value(), 0, 16));
++offset;
}
catch (...) {
return false;
}
}
break;
case TEXTURE_BEGIN:
if (tkn->type() == tcl_t::ATOM && tkn->value() == "{") {
st = TEXTURE_KEY;
}
break;
case TEXTURE_KEY:
if (tkn->type() == tcl_t::OTHER) {
st = TEXTURE_VALUE;
txtKey = tkn->value();
}
else if (tkn->type() == tcl_t::ATOM && tkn->value() == "}") {
st = ANY_KEY;
}
else {
return false;
}
break;
case TEXTURE_VALUE:
if (tkn->type() == tcl_t::STRING) {
st = TEXTURE_KEY;
m_textureList[txtKey] = tkn->value();
}
else {
return false;
}
break;
} // switch st
}
break;
}
} // while
return false;
}