-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
p_mapinfo.cpp
296 lines (262 loc) · 10.8 KB
/
p_mapinfo.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/** @file p_mapinfo.cpp Hexen MAPINFO parsing.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Activision
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "jhexen.h"
#include "g_common.h"
#include "p_mapinfo.h"
#include "hexlex.h"
#include <cstdio>
#include <sstream>
#include <string.h>
#include <map>
#define MUSIC_STARTUP "startup"
#define MUSIC_ENDING1 "hall"
#define MUSIC_ENDING2 "orb"
#define MUSIC_ENDING3 "chess"
#define MUSIC_INTERMISSION "hub"
#define MUSIC_TITLE "hexen"
typedef std::map<std::string, mapinfo_t> MapInfos;
static MapInfos mapInfos;
/**
* Update the Music definition @a musicId with the specified CD @a track number.
*/
static void setMusicCDTrack(char const *musicId, int track)
{
App_Log(DE2_DEV_RES_VERBOSE, "setMusicCDTrack: musicId=%s, track=%i", musicId, track);
int cdTrack = track;
Def_Set(DD_DEF_MUSIC, Def_Get(DD_DEF_MUSIC, musicId, 0), DD_CD_TRACK, &cdTrack);
}
void MapInfoParser(Str const *path)
{
mapInfos.clear();
uint logicalMapIndex = 0;
// Prepare a default-configured definition, for one-shot initialization.
mapinfo_t defMapInfo;
defMapInfo.map = -1; // Unknown.
defMapInfo.cluster = 0;
defMapInfo.warpTrans = 0;
defMapInfo.nextMap = 0; // Always go to map 0 if not specified.
defMapInfo.cdTrack = 1;
defMapInfo.sky1Material = Materials_ResolveUriCString(gameMode == hexen_demo || gameMode == hexen_betademo? "Textures:SKY2" : "Textures:SKY1");
defMapInfo.sky2Material = defMapInfo.sky1Material;
defMapInfo.sky1ScrollDelta = 0;
defMapInfo.sky2ScrollDelta = 0;
defMapInfo.doubleSky = false;
defMapInfo.lightning = false;
defMapInfo.fadeTable = W_GetLumpNumForName("COLORMAP");
strcpy(defMapInfo.title, "DEVELOPMENT MAP"); // Unknown.
strcpy(defMapInfo.songLump, "DEFSONG"); // Unknown.
AutoStr *script = M_ReadFileIntoString(path, 0);
if(script && !Str_IsEmpty(script))
{
App_Log(DE2_RES_VERBOSE, "Parsing \"%s\"...", F_PrettyPath(Str_Text(path)));
HexLex lexer(script, path);
while(lexer.readToken())
{
if(!Str_CompareIgnoreCase(lexer.token(), "cd_start_track"))
{
setMusicCDTrack(MUSIC_STARTUP, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cd_end1_track"))
{
setMusicCDTrack(MUSIC_ENDING1, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cd_end2_track"))
{
setMusicCDTrack(MUSIC_ENDING2, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cd_end3_track"))
{
setMusicCDTrack(MUSIC_ENDING3, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cd_intermission_track"))
{
setMusicCDTrack(MUSIC_INTERMISSION, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cd_title_track"))
{
setMusicCDTrack(MUSIC_TITLE, lexer.readNumber());
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "map"))
{
int tmap = lexer.readNumber();
if(tmap < 1)
{
Con_Error("MapInfoParser: Invalid map number '%s' in \"%s\" on line #%i",
lexer.token(), F_PrettyPath(Str_Text(path)), lexer.lineNumber());
}
Uri *mapUri = G_ComposeMapUri(0, tmap - 1);
mapinfo_t *info = P_MapInfo(mapUri);
if(!info)
{
// A new map info.
info = &mapInfos[Str_Text(Uri_Compose(mapUri))];
// Initialize with the default values.
std::memcpy(info, &defMapInfo, sizeof(*info));
// Assign a logical map index.
info->map = logicalMapIndex++;
// The warp translation defaults to the map number.
info->warpTrans = tmap - 1;
}
Uri_Delete(mapUri);
// Map title must follow the number.
strcpy(info->title, Str_Text(lexer.readString()));
// Process optional tokens.
while(lexer.readToken())
{
if(!Str_CompareIgnoreCase(lexer.token(), "sky1"))
{
Uri *uri = lexer.readUri("Textures");
info->sky1Material = Materials_ResolveUri(uri);
info->sky1ScrollDelta = (float) lexer.readNumber() / 256;
Uri_Delete(uri);
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "sky2"))
{
Uri *uri = lexer.readUri("Textures");
info->sky2Material = Materials_ResolveUri(uri);
info->sky2ScrollDelta = (float) lexer.readNumber() / 256;
Uri_Delete(uri);
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "doublesky"))
{
info->doubleSky = true;
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "lightning"))
{
info->lightning = true;
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "fadetable"))
{
info->fadeTable = W_GetLumpNumForName(Str_Text(lexer.readString()));
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cluster"))
{
info->cluster = lexer.readNumber();
if(info->cluster < 1)
{
Con_Error("MapInfoParser: Invalid cluster number '%s' in \"%s\" on line #%i",
lexer.token(), F_PrettyPath(Str_Text(path)), lexer.lineNumber());
}
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "warptrans"))
{
int mapWarpNum = lexer.readNumber();
if(mapWarpNum < 1)
{
Con_Error("MapInfoParser: Invalid map warp-number '%s' in \"%s\" on line #%i",
lexer.token(), F_PrettyPath(Str_Text(path)), lexer.lineNumber());
}
info->warpTrans = (unsigned) mapWarpNum - 1;
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "next"))
{
int map = lexer.readNumber();
if(map < 1)
{
Con_Error("MapInfoParser: Invalid map number '%s' in \"%s\" on line #%i",
lexer.token(), F_PrettyPath(Str_Text(path)), lexer.lineNumber());
}
info->nextMap = (unsigned) map - 1;
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "cdtrack"))
{
info->cdTrack = lexer.readNumber();
continue;
}
lexer.unreadToken();
break;
}
continue;
}
// Found an unexpected token.
Con_Error("MapInfoParser: Unexpected token '%s' in \"%s\" on line #%i",
lexer.token(), F_PrettyPath(Str_Text(path)), lexer.lineNumber());
}
}
else
{
App_Log(DE2_RES_WARNING, "MapInfoParser: Failed to open definition/script file \"%s\" for reading", F_PrettyPath(Str_Text(path)));
}
#ifdef DENG_DEBUG
for(MapInfos::const_iterator i = mapInfos.begin(); i != mapInfos.end(); ++i)
{
mapinfo_t const &info = i->second;
App_Log(DE2_DEV_RES_MSG, "MAPINFO %s { title: \"%s\" map: %i warp: %i }",
i->first.c_str(), info.title, info.map, info.warpTrans);
}
#endif
}
mapinfo_t *P_MapInfo(Uri const *mapUri)
{
DENG_ASSERT(mapUri != 0);
std::string mapPath(Str_Text(Uri_Compose(mapUri)));
MapInfos::iterator found = mapInfos.find(mapPath);
if(found != mapInfos.end())
{
return &found->second;
}
return 0;
}
uint P_TranslateMapIfExists(uint map)
{
uint matchedWithoutCluster = P_INVALID_LOGICAL_MAP;
for(MapInfos::const_iterator i = mapInfos.begin(); i != mapInfos.end(); ++i)
{
mapinfo_t const &info = i->second;
if(info.warpTrans == map)
{
if(info.cluster)
{
App_Log(DE2_DEV_MAP_VERBOSE, "Warp %i translated to logical map %i, cluster %i", map, info.map, info.cluster);
return info.map;
}
App_Log(DE2_DEV_MAP_VERBOSE, "Warp %i matches logical map %i, but it has no cluster", map, info.map);
matchedWithoutCluster = info.map;
}
}
App_Log(DE2_DEV_MAP_NOTE, "Could not find warp %i, translating to logical map %i (without cluster)",
map, matchedWithoutCluster);
return matchedWithoutCluster;
}
uint P_TranslateMap(uint map)
{
uint translated = P_TranslateMapIfExists(map);
if(translated == P_INVALID_LOGICAL_MAP)
{
// This function always returns a valid logical map.
return 0;
}
return translated;
}