| 1 |
// |
| 2 |
// Copyright(C) 2023 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA MAPINFO Doom Parser |
| 16 |
// |
| 17 |
|
| 18 |
#include <cstring> |
| 19 |
#include <cstdio> |
| 20 |
#include <vector> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
extern "C" { |
| 24 |
#include "doomstat.h" |
| 25 |
#include "z_zone.h" |
| 26 |
|
| 27 |
#include "dsda/name.h" |
| 28 |
#include "dsda/utility.h" |
| 29 |
} |
| 30 |
|
| 31 |
#include "scanner.h" |
| 32 |
|
| 33 |
#include "parser.h" |
| 34 |
|
| 35 |
static std::vector<doom_mapinfo_map_t> doom_mapinfo_maps; |
| 36 |
static doom_mapinfo_map_t default_map; |
| 37 |
|
| 38 |
static std::vector<doom_mapinfo_skill_t> doom_mapinfo_skills; |
| 39 |
static std::vector<doom_mapinfo_cluster_t> doom_mapinfo_clusters; |
| 40 |
static std::vector<doom_mapinfo_episode_t> doom_mapinfo_episodes; |
| 41 |
|
| 42 |
static void dsda_SkipValue(Scanner &scanner) { |
| 43 |
if (scanner.CheckToken('=')) { |
| 44 |
do { |
| 45 |
scanner.GetNextToken(); |
| 46 |
} while (scanner.CheckToken(',')); |
| 47 |
|
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if (scanner.CheckToken('{')) |
| 52 |
{ |
| 53 |
int brace_count = 1; |
| 54 |
|
| 55 |
while (scanner.TokensLeft()) { |
| 56 |
if (scanner.CheckToken('}')) { |
| 57 |
--brace_count; |
| 58 |
} |
| 59 |
else if (scanner.CheckToken('{')) { |
| 60 |
++brace_count; |
| 61 |
} |
| 62 |
|
| 63 |
if (!brace_count) |
| 64 |
break; |
| 65 |
|
| 66 |
scanner.GetNextToken(); |
| 67 |
} |
| 68 |
|
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#define STR_DUP(x) { Z_Free(x); x = Z_Strdup(scanner.string); } |
| 74 |
#define RESET_STR(x) { Z_Free(x); x = NULL; } |
| 75 |
|
| 76 |
// The scanner drops the sign when scanning, and we need it back |
| 77 |
static void dsda_FloatString(Scanner &scanner, char* &str) { |
| 78 |
if (scanner.decimal >= 0) { |
| 79 |
STR_DUP(str); |
| 80 |
} |
| 81 |
else { |
| 82 |
str = (char*) Z_Realloc(str, strlen(scanner.string) + 2); |
| 83 |
str[0] = '-'; |
| 84 |
str[1] = '\0'; |
| 85 |
strcat(str, scanner.string); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#define SCAN_INT(x) { scanner.MustGetToken('='); \ |
| 90 |
scanner.MustGetInteger(); \ |
| 91 |
x = scanner.number; } |
| 92 |
|
| 93 |
#define SCAN_FLOAT(x) { scanner.MustGetToken('='); \ |
| 94 |
scanner.MustGetFloat(); \ |
| 95 |
x = scanner.decimal; } |
| 96 |
|
| 97 |
#define SCAN_STRING(x) { scanner.MustGetToken('='); \ |
| 98 |
scanner.MustGetString(); \ |
| 99 |
STR_DUP(x); } |
| 100 |
|
| 101 |
#define SCAN_FLOAT_STRING(x) { scanner.MustGetToken('='); \ |
| 102 |
scanner.MustGetFloat(); \ |
| 103 |
dsda_FloatString(scanner, x); } |
| 104 |
|
| 105 |
static void dsda_ParseDoomMapInfoMultiString(Scanner &scanner, char* &str) { |
| 106 |
scanner.MustGetToken('='); |
| 107 |
scanner.MustGetString(); |
| 108 |
std::string multi_str(scanner.string); |
| 109 |
|
| 110 |
while (scanner.CheckToken(',')) { |
| 111 |
scanner.MustGetString(); |
| 112 |
multi_str += "\n"; |
| 113 |
multi_str += scanner.string; |
| 114 |
} |
| 115 |
|
| 116 |
Z_Free(str); |
| 117 |
str = Z_Strdup(multi_str.c_str()); |
| 118 |
} |
| 119 |
|
| 120 |
static void dsda_ResetMap(doom_mapinfo_map_t &map) { |
| 121 |
Z_Free(map.lump_name); |
| 122 |
Z_Free(map.nice_name); |
| 123 |
Z_Free(map.title_patch); |
| 124 |
Z_Free(map.music); |
| 125 |
Z_Free(map.inter_music); |
| 126 |
Z_Free(map.exit_pic); |
| 127 |
Z_Free(map.enter_pic); |
| 128 |
Z_Free(map.border_texture); |
| 129 |
Z_Free(map.gravity); |
| 130 |
Z_Free(map.air_control); |
| 131 |
Z_Free(map.author); |
| 132 |
Z_Free(map.colormap); |
| 133 |
Z_Free(map.special_actions); |
| 134 |
|
| 135 |
Z_Free(map.next.map); |
| 136 |
Z_Free(map.next.end_pic); |
| 137 |
Z_Free(map.next.end_pic_b); |
| 138 |
Z_Free(map.next.music); |
| 139 |
Z_Free(map.secret_next.map); |
| 140 |
Z_Free(map.secret_next.end_pic); |
| 141 |
Z_Free(map.secret_next.end_pic_b); |
| 142 |
Z_Free(map.secret_next.music); |
| 143 |
|
| 144 |
Z_Free(map.sky1.lump); |
| 145 |
|
| 146 |
memset(&map, 0, sizeof(map)); |
| 147 |
} |
| 148 |
|
| 149 |
#define REPLACE_WITH_COPY(x) { if (x) x = Z_Strdup(x); } |
| 150 |
|
| 151 |
static void dsda_CopyMap(doom_mapinfo_map_t &dest, doom_mapinfo_map_t &source) { |
| 152 |
dest = source; |
| 153 |
|
| 154 |
REPLACE_WITH_COPY(dest.lump_name); |
| 155 |
REPLACE_WITH_COPY(dest.nice_name); |
| 156 |
REPLACE_WITH_COPY(dest.title_patch); |
| 157 |
REPLACE_WITH_COPY(dest.music); |
| 158 |
REPLACE_WITH_COPY(dest.inter_music); |
| 159 |
REPLACE_WITH_COPY(dest.exit_pic); |
| 160 |
REPLACE_WITH_COPY(dest.enter_pic); |
| 161 |
REPLACE_WITH_COPY(dest.border_texture); |
| 162 |
REPLACE_WITH_COPY(dest.gravity); |
| 163 |
REPLACE_WITH_COPY(dest.air_control); |
| 164 |
REPLACE_WITH_COPY(dest.author); |
| 165 |
REPLACE_WITH_COPY(dest.colormap); |
| 166 |
|
| 167 |
REPLACE_WITH_COPY(dest.next.map); |
| 168 |
REPLACE_WITH_COPY(dest.next.end_pic); |
| 169 |
REPLACE_WITH_COPY(dest.next.end_pic_b); |
| 170 |
REPLACE_WITH_COPY(dest.next.music); |
| 171 |
REPLACE_WITH_COPY(dest.secret_next.map); |
| 172 |
REPLACE_WITH_COPY(dest.secret_next.end_pic); |
| 173 |
REPLACE_WITH_COPY(dest.secret_next.end_pic_b); |
| 174 |
REPLACE_WITH_COPY(dest.secret_next.music); |
| 175 |
|
| 176 |
REPLACE_WITH_COPY(dest.sky1.lump); |
| 177 |
|
| 178 |
dest.num_special_actions = 0; |
| 179 |
dest.special_actions = NULL; |
| 180 |
} |
| 181 |
|
| 182 |
static void dsda_ParseDoomMapInfoMapNext(Scanner &scanner, doom_mapinfo_map_next_t &next) { |
| 183 |
scanner.MustGetToken('='); |
| 184 |
|
| 185 |
RESET_STR(next.map); |
| 186 |
RESET_STR(next.end_pic); |
| 187 |
RESET_STR(next.end_pic_b); |
| 188 |
RESET_STR(next.music); |
| 189 |
next.loop_music = true; |
| 190 |
next.end = dmi_end_null; |
| 191 |
|
| 192 |
scanner.MustGetString(); |
| 193 |
|
| 194 |
if (scanner.StringMatch("EndGame1")) { |
| 195 |
next.end_pic = Z_Strdup(gamemode == retail ? "CREDIT" : "HELP2"); |
| 196 |
} |
| 197 |
else if (scanner.StringMatch("EndGame2")) { |
| 198 |
next.end_pic = Z_Strdup("VICTORY2"); |
| 199 |
} |
| 200 |
else if (scanner.StringMatch("EndGame4")) { |
| 201 |
next.end_pic = Z_Strdup("ENDPIC"); |
| 202 |
} |
| 203 |
else if (scanner.StringMatch("EndGame3")) { |
| 204 |
next.end = dmi_end_game_scroll; |
| 205 |
} |
| 206 |
else if (scanner.StringMatch("EndGameC")) { |
| 207 |
next.end = dmi_end_game_cast; |
| 208 |
} |
| 209 |
else if (scanner.StringMatch("EndPic")) { |
| 210 |
scanner.MustGetToken(','); |
| 211 |
scanner.MustGetString(); |
| 212 |
STR_DUP(next.end_pic); |
| 213 |
} |
| 214 |
else if (scanner.StringMatch("endgame")) { |
| 215 |
scanner.MustGetToken('{'); |
| 216 |
while (!scanner.CheckToken('}')) { |
| 217 |
scanner.MustGetToken(TK_Identifier); |
| 218 |
|
| 219 |
if (scanner.StringMatch("pic")) { |
| 220 |
SCAN_STRING(next.end_pic); |
| 221 |
} |
| 222 |
else if (scanner.StringMatch("hscroll")) { |
| 223 |
next.end = dmi_end_game_scroll; |
| 224 |
scanner.MustGetToken('='); |
| 225 |
scanner.MustGetString(); |
| 226 |
STR_DUP(next.end_pic); |
| 227 |
scanner.MustGetToken(','); |
| 228 |
scanner.MustGetString(); |
| 229 |
STR_DUP(next.end_pic_b); |
| 230 |
} |
| 231 |
else if (scanner.StringMatch("cast")) { |
| 232 |
next.end = dmi_end_game_cast; |
| 233 |
} |
| 234 |
else if (scanner.StringMatch("music")) { |
| 235 |
SCAN_STRING(next.music); |
| 236 |
if (scanner.CheckToken(',')) { |
| 237 |
scanner.MustGetInteger(); |
| 238 |
next.loop_music = !!scanner.number; |
| 239 |
} |
| 240 |
} |
| 241 |
else { |
| 242 |
dsda_SkipValue(scanner); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
else { |
| 247 |
STR_DUP(next.map); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static void dsda_ParseDoomMapInfoMapSky(Scanner &scanner, doom_mapinfo_sky_t &sky) { |
| 252 |
scanner.MustGetToken('='); |
| 253 |
scanner.MustGetString(); |
| 254 |
|
| 255 |
STR_DUP(sky.lump); |
| 256 |
} |
| 257 |
|
| 258 |
static void dsda_ParseDoomMapInfoTitlePatch(Scanner &scanner, doom_mapinfo_map_t &map) { |
| 259 |
scanner.MustGetToken('='); |
| 260 |
scanner.MustGetString(); |
| 261 |
|
| 262 |
STR_DUP(map.title_patch); |
| 263 |
if (scanner.CheckToken(',')) { |
| 264 |
scanner.MustGetInteger(); |
| 265 |
if (scanner.number) |
| 266 |
map.flags &= ~DMI_SHOW_AUTHOR; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
static void dsda_ParseDoomMapInfoMusic(Scanner &scanner, doom_mapinfo_map_t &map) { |
| 271 |
scanner.MustGetToken('='); |
| 272 |
scanner.MustGetString(); |
| 273 |
|
| 274 |
if (strchr(scanner.string, ':')) |
| 275 |
return; |
| 276 |
|
| 277 |
if (scanner.CheckToken(',')) { |
| 278 |
scanner.MustGetInteger(); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
STR_DUP(map.music); |
| 283 |
} |
| 284 |
|
| 285 |
static void dsda_ParseDoomMapInfoIntermissionPic(Scanner &scanner, char* &pic) { |
| 286 |
scanner.MustGetToken('='); |
| 287 |
scanner.MustGetString(); |
| 288 |
|
| 289 |
if (scanner.string[0] == '$') |
| 290 |
return; |
| 291 |
|
| 292 |
STR_DUP(pic); |
| 293 |
} |
| 294 |
|
| 295 |
static void dsda_ParseDoomMapInfoMapSpecialAction(Scanner &scanner, |
| 296 |
std::vector<doom_mapinfo_special_action_t> &special_actions) { |
| 297 |
doom_mapinfo_special_action_t special_action = { 0 }; |
| 298 |
|
| 299 |
scanner.MustGetToken('='); |
| 300 |
|
| 301 |
scanner.MustGetString(); |
| 302 |
special_action.monster_type = dsda_ActorNameToType(scanner.string); |
| 303 |
|
| 304 |
scanner.MustGetToken(','); |
| 305 |
|
| 306 |
scanner.MustGetString(); |
| 307 |
special_action.action_special = dsda_ActionNameToNumber(scanner.string); |
| 308 |
|
| 309 |
for (int i = 0; i < 5; ++i) { |
| 310 |
if (!scanner.CheckToken(',')) |
| 311 |
break; |
| 312 |
|
| 313 |
scanner.MustGetInteger(); |
| 314 |
special_action.special_args[i] = scanner.number; |
| 315 |
} |
| 316 |
|
| 317 |
if (special_action.monster_type == NAME_NOT_FOUND || |
| 318 |
special_action.action_special == NAME_NOT_FOUND) |
| 319 |
return; |
| 320 |
|
| 321 |
special_actions.push_back(special_action); |
| 322 |
} |
| 323 |
|
| 324 |
static void dsda_GuessLevelNum(doom_mapinfo_map_t &map) { |
| 325 |
int level, episode; |
| 326 |
|
| 327 |
dsda_UppercaseString(map.lump_name); |
| 328 |
|
| 329 |
if (sscanf(map.lump_name, "MAP%d", &level) == 1) |
| 330 |
map.level_num = level; |
| 331 |
else if (sscanf(map.lump_name, "E%dM%d", &episode, &level) == 2) |
| 332 |
map.level_num = (episode - 1) * 10 + level; |
| 333 |
else |
| 334 |
map.level_num = -1; |
| 335 |
} |
| 336 |
|
| 337 |
static void dsda_ParseDoomMapInfoMapBlock(Scanner &scanner, doom_mapinfo_map_t &map, |
| 338 |
std::vector<doom_mapinfo_special_action_t> &special_actions) { |
| 339 |
scanner.MustGetToken('{'); |
| 340 |
while (!scanner.CheckToken('}')) { |
| 341 |
scanner.MustGetToken(TK_Identifier); |
| 342 |
|
| 343 |
if (scanner.StringMatch("LevelNum")) { |
| 344 |
SCAN_INT(map.level_num); |
| 345 |
} |
| 346 |
else if (scanner.StringMatch("Next")) { |
| 347 |
dsda_ParseDoomMapInfoMapNext(scanner, map.next); |
| 348 |
} |
| 349 |
else if (scanner.StringMatch("SecretNext")) { |
| 350 |
dsda_ParseDoomMapInfoMapNext(scanner, map.secret_next); |
| 351 |
} |
| 352 |
else if (scanner.StringMatch("Cluster")) { |
| 353 |
SCAN_INT(map.cluster); |
| 354 |
} |
| 355 |
else if (scanner.StringMatch("Sky1")) { |
| 356 |
dsda_ParseDoomMapInfoMapSky(scanner, map.sky1); |
| 357 |
} |
| 358 |
else if (scanner.StringMatch("TitlePatch")) { |
| 359 |
dsda_ParseDoomMapInfoTitlePatch(scanner, map); |
| 360 |
} |
| 361 |
else if (scanner.StringMatch("Par")) { |
| 362 |
SCAN_INT(map.par); |
| 363 |
} |
| 364 |
else if (scanner.StringMatch("NoIntermission")) { |
| 365 |
map.flags &= ~DMI_INTERMISSION; |
| 366 |
} |
| 367 |
else if (scanner.StringMatch("Intermission")) { |
| 368 |
map.flags |= DMI_INTERMISSION; |
| 369 |
} |
| 370 |
else if (scanner.StringMatch("Music")) { |
| 371 |
dsda_ParseDoomMapInfoMusic(scanner, map); |
| 372 |
} |
| 373 |
else if (scanner.StringMatch("ExitPic")) { |
| 374 |
dsda_ParseDoomMapInfoIntermissionPic(scanner, map.exit_pic); |
| 375 |
} |
| 376 |
else if (scanner.StringMatch("EnterPic")) { |
| 377 |
dsda_ParseDoomMapInfoIntermissionPic(scanner, map.enter_pic); |
| 378 |
} |
| 379 |
else if (scanner.StringMatch("InterMusic")) { |
| 380 |
SCAN_STRING(map.inter_music); |
| 381 |
} |
| 382 |
else if (scanner.StringMatch("BorderTexture")) { |
| 383 |
SCAN_STRING(map.border_texture); |
| 384 |
} |
| 385 |
else if (scanner.StringMatch("EvenLighting")) { |
| 386 |
map.flags |= DMI_EVEN_LIGHTING; |
| 387 |
map.flags &= ~DMI_SMOOTH_LIGHTING; |
| 388 |
} |
| 389 |
else if (scanner.StringMatch("SmoothLighting")) { |
| 390 |
map.flags |= DMI_SMOOTH_LIGHTING; |
| 391 |
map.flags &= ~DMI_EVEN_LIGHTING; |
| 392 |
} |
| 393 |
else if (scanner.StringMatch("Gravity")) { |
| 394 |
SCAN_FLOAT_STRING(map.gravity); |
| 395 |
} |
| 396 |
else if (scanner.StringMatch("AirControl")) { |
| 397 |
SCAN_FLOAT_STRING(map.air_control); |
| 398 |
} |
| 399 |
else if (scanner.StringMatch("AllowMonsterTelefrags")) { |
| 400 |
map.flags |= DMI_ALLOW_MONSTER_TELEFRAGS; |
| 401 |
} |
| 402 |
else if (scanner.StringMatch("KillerActivatesDeathSpecials")) { |
| 403 |
map.flags &= ~DMI_ACTIVATE_OWN_DEATH_SPECIALS; |
| 404 |
} |
| 405 |
else if (scanner.StringMatch("ActivateOwnDeathSpecials")) { |
| 406 |
map.flags |= DMI_ACTIVATE_OWN_DEATH_SPECIALS; |
| 407 |
} |
| 408 |
else if (scanner.StringMatch("SpecialAction")) { |
| 409 |
dsda_ParseDoomMapInfoMapSpecialAction(scanner, special_actions); |
| 410 |
} |
| 411 |
else if (scanner.StringMatch("StrictMonsterActivation")) { |
| 412 |
map.flags &= ~DMI_LAX_MONSTER_ACTIVATION; |
| 413 |
} |
| 414 |
else if (scanner.StringMatch("LaxMonsterActivation")) { |
| 415 |
map.flags |= DMI_LAX_MONSTER_ACTIVATION; |
| 416 |
} |
| 417 |
else if (scanner.StringMatch("MissileShootersActivateImpactLines")) { |
| 418 |
map.flags &= ~DMI_MISSILES_ACTIVATE_IMPACT_LINES; |
| 419 |
} |
| 420 |
else if (scanner.StringMatch("MissilesActivateImpactLines")) { |
| 421 |
map.flags |= DMI_MISSILES_ACTIVATE_IMPACT_LINES; |
| 422 |
} |
| 423 |
else if (scanner.StringMatch("FilterStarts")) { |
| 424 |
map.flags |= DMI_FILTER_STARTS; |
| 425 |
} |
| 426 |
else if (scanner.StringMatch("AllowRespawn")) { |
| 427 |
map.flags |= DMI_ALLOW_RESPAWN; |
| 428 |
} |
| 429 |
else if (scanner.StringMatch("NoJump")) { |
| 430 |
map.flags &= ~DMI_ALLOW_JUMP; |
| 431 |
} |
| 432 |
else if (scanner.StringMatch("AllowJump")) { |
| 433 |
map.flags |= DMI_ALLOW_JUMP; |
| 434 |
} |
| 435 |
else if (scanner.StringMatch("NoFreelook")) { |
| 436 |
map.flags &= ~DMI_ALLOW_FREE_LOOK; |
| 437 |
} |
| 438 |
else if (scanner.StringMatch("AllowFreelook")) { |
| 439 |
map.flags |= DMI_ALLOW_FREE_LOOK; |
| 440 |
} |
| 441 |
else if (scanner.StringMatch("NoCheckSwitchRange")) { |
| 442 |
map.flags &= ~DMI_CHECK_SWITCH_RANGE; |
| 443 |
} |
| 444 |
else if (scanner.StringMatch("CheckSwitchRange")) { |
| 445 |
map.flags |= DMI_CHECK_SWITCH_RANGE; |
| 446 |
} |
| 447 |
else if (scanner.StringMatch("ResetHealth")) { |
| 448 |
map.flags |= DMI_RESET_HEALTH; |
| 449 |
} |
| 450 |
else if (scanner.StringMatch("ResetInventory")) { |
| 451 |
map.flags |= DMI_RESET_INVENTORY; |
| 452 |
} |
| 453 |
else if (scanner.StringMatch("NoPassover")) { |
| 454 |
map.flags &= ~DMI_PASSOVER; |
| 455 |
} |
| 456 |
else if (scanner.StringMatch("Passover")) { |
| 457 |
map.flags |= DMI_PASSOVER; |
| 458 |
} |
| 459 |
else if (scanner.StringMatch("NoGravity")) { |
| 460 |
map.gravity = Z_Strdup("0"); |
| 461 |
} |
| 462 |
else if (scanner.StringMatch("UsePlayerStartZ")) { |
| 463 |
map.flags |= DMI_USE_PLAYER_START_Z; |
| 464 |
} |
| 465 |
else if (scanner.StringMatch("NoVerticalExplosionThrust")) { |
| 466 |
map.flags &= ~DMI_VERTICAL_EXPLOSION_THRUST; |
| 467 |
} |
| 468 |
else if (scanner.StringMatch("VerticalExplosionThrust")) { |
| 469 |
map.flags |= DMI_VERTICAL_EXPLOSION_THRUST; |
| 470 |
} |
| 471 |
else if (scanner.StringMatch("ExplodeIn2D")) { |
| 472 |
map.flags &= ~DMI_EXPLODE_IN_3D; |
| 473 |
} |
| 474 |
else if (scanner.StringMatch("ExplodeIn3D")) { |
| 475 |
map.flags |= DMI_EXPLODE_IN_3D; |
| 476 |
} |
| 477 |
else if (scanner.StringMatch("Author")) { |
| 478 |
SCAN_STRING(map.author); |
| 479 |
} |
| 480 |
else if (scanner.StringMatch("ColorMap")) { |
| 481 |
SCAN_STRING(map.colormap); |
| 482 |
} |
| 483 |
else { |
| 484 |
dsda_SkipValue(scanner); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
map.num_special_actions = special_actions.size(); |
| 489 |
map.special_actions = (doom_mapinfo_special_action_t *) Z_Realloc( |
| 490 |
map.special_actions, map.num_special_actions * sizeof(*map.special_actions) |
| 491 |
); |
| 492 |
|
| 493 |
if(map.num_special_actions > 0) { |
| 494 |
memcpy(map.special_actions, &special_actions[0], |
| 495 |
map.num_special_actions * sizeof(*map.special_actions)); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
static void dsda_ParseDoomMapInfoMap(Scanner &scanner) { |
| 500 |
doom_mapinfo_map_t map; |
| 501 |
std::vector<doom_mapinfo_special_action_t> special_actions; |
| 502 |
|
| 503 |
// Create a copy of the existing special actions from the default map |
| 504 |
for (int i = 0; i < default_map.num_special_actions; ++i) |
| 505 |
special_actions.push_back(default_map.special_actions[i]); |
| 506 |
|
| 507 |
dsda_CopyMap(map, default_map); |
| 508 |
|
| 509 |
scanner.MustGetString(); |
| 510 |
STR_DUP(map.lump_name); |
| 511 |
|
| 512 |
scanner.MustGetString(); |
| 513 |
STR_DUP(map.nice_name); |
| 514 |
|
| 515 |
dsda_GuessLevelNum(map); |
| 516 |
|
| 517 |
dsda_ParseDoomMapInfoMapBlock(scanner, map, special_actions); |
| 518 |
|
| 519 |
for (auto &old_map : doom_mapinfo_maps) |
| 520 |
if (old_map.level_num == map.level_num) |
| 521 |
old_map.level_num = 0; |
| 522 |
|
| 523 |
for (auto &old_map : doom_mapinfo_maps) |
| 524 |
if (!stricmp(old_map.lump_name, map.lump_name)) { |
| 525 |
dsda_ResetMap(old_map); |
| 526 |
old_map = map; |
| 527 |
|
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
doom_mapinfo_maps.push_back(map); |
| 532 |
} |
| 533 |
|
| 534 |
static void dsda_InitDefaultMap(void) { |
| 535 |
dsda_ResetMap(default_map); |
| 536 |
|
| 537 |
default_map.gravity = Z_Strdup("800"); |
| 538 |
default_map.flags = DMI_INTERMISSION | |
| 539 |
DMI_ACTIVATE_OWN_DEATH_SPECIALS | |
| 540 |
DMI_LAX_MONSTER_ACTIVATION | |
| 541 |
DMI_MISSILES_ACTIVATE_IMPACT_LINES | |
| 542 |
DMI_SHOW_AUTHOR; |
| 543 |
} |
| 544 |
|
| 545 |
static void dsda_ParseDoomMapInfoDefaultMap(Scanner &scanner) { |
| 546 |
std::vector<doom_mapinfo_special_action_t> special_actions; |
| 547 |
|
| 548 |
dsda_InitDefaultMap(); |
| 549 |
|
| 550 |
dsda_ParseDoomMapInfoMapBlock(scanner, default_map, special_actions); |
| 551 |
} |
| 552 |
|
| 553 |
static void dsda_ParseDoomMapInfoAddDefaultMap(Scanner &scanner) { |
| 554 |
std::vector<doom_mapinfo_special_action_t> special_actions; |
| 555 |
|
| 556 |
// Create a copy of the existing special actions from the default map |
| 557 |
for (int i = 0; i < default_map.num_special_actions; ++i) |
| 558 |
special_actions.push_back(default_map.special_actions[i]); |
| 559 |
|
| 560 |
Z_Free(default_map.special_actions); |
| 561 |
default_map.special_actions = NULL; |
| 562 |
default_map.num_special_actions = 0; |
| 563 |
|
| 564 |
dsda_ParseDoomMapInfoMapBlock(scanner, default_map, special_actions); |
| 565 |
} |
| 566 |
|
| 567 |
static void dsda_FreeSkill(doom_mapinfo_skill_t &skill) { |
| 568 |
Z_Free(skill.unique_id); |
| 569 |
Z_Free(skill.ammo_factor); |
| 570 |
Z_Free(skill.damage_factor); |
| 571 |
Z_Free(skill.armor_factor); |
| 572 |
Z_Free(skill.health_factor); |
| 573 |
Z_Free(skill.monster_health_factor); |
| 574 |
Z_Free(skill.friend_health_factor); |
| 575 |
Z_Free(skill.must_confirm); |
| 576 |
Z_Free(skill.name); |
| 577 |
Z_Free(skill.pic_name); |
| 578 |
Z_Free(skill.text_color); |
| 579 |
} |
| 580 |
|
| 581 |
static void dsda_ParseDoomMapInfoSkill(Scanner &scanner) { |
| 582 |
doom_mapinfo_skill_t skill = { 0 }; |
| 583 |
|
| 584 |
scanner.MustGetString(); |
| 585 |
STR_DUP(skill.unique_id); |
| 586 |
|
| 587 |
scanner.MustGetToken('{'); |
| 588 |
while (!scanner.CheckToken('}')) { |
| 589 |
scanner.MustGetToken(TK_Identifier); |
| 590 |
|
| 591 |
if (scanner.StringMatch("AmmoFactor")) { |
| 592 |
SCAN_FLOAT_STRING(skill.ammo_factor); |
| 593 |
} |
| 594 |
else if (scanner.StringMatch("DamageFactor")) { |
| 595 |
SCAN_FLOAT_STRING(skill.damage_factor); |
| 596 |
} |
| 597 |
else if (scanner.StringMatch("ArmorFactor")) { |
| 598 |
SCAN_FLOAT_STRING(skill.armor_factor); |
| 599 |
} |
| 600 |
else if (scanner.StringMatch("HealthFactor")) { |
| 601 |
SCAN_FLOAT_STRING(skill.health_factor); |
| 602 |
} |
| 603 |
else if (scanner.StringMatch("MonsterHealth")) { |
| 604 |
SCAN_FLOAT_STRING(skill.monster_health_factor); |
| 605 |
} |
| 606 |
else if (scanner.StringMatch("FriendlyHealth")) { |
| 607 |
SCAN_FLOAT_STRING(skill.friend_health_factor); |
| 608 |
} |
| 609 |
else if (scanner.StringMatch("RespawnTime")) { |
| 610 |
SCAN_INT(skill.respawn_time); |
| 611 |
} |
| 612 |
else if (scanner.StringMatch("SpawnFilter")) { |
| 613 |
SCAN_INT(skill.spawn_filter); |
| 614 |
} |
| 615 |
else if (scanner.StringMatch("Key")) { |
| 616 |
scanner.MustGetToken('='); |
| 617 |
scanner.MustGetString(); |
| 618 |
skill.key = tolower(scanner.string[0]); |
| 619 |
} |
| 620 |
else if (scanner.StringMatch("MustConfirm")) { |
| 621 |
if (scanner.CheckToken('=')) { |
| 622 |
scanner.MustGetString(); |
| 623 |
STR_DUP(skill.must_confirm); |
| 624 |
} |
| 625 |
|
| 626 |
skill.flags |= DSI_MUST_CONFIRM; |
| 627 |
} |
| 628 |
else if (scanner.StringMatch("Name")) { |
| 629 |
SCAN_STRING(skill.name); |
| 630 |
} |
| 631 |
else if (scanner.StringMatch("PicName")) { |
| 632 |
SCAN_STRING(skill.pic_name); |
| 633 |
} |
| 634 |
else if (scanner.StringMatch("TextColor")) { |
| 635 |
SCAN_STRING(skill.text_color); |
| 636 |
} |
| 637 |
else if (scanner.StringMatch("SpawnMulti")) { |
| 638 |
skill.flags |= DSI_SPAWN_MULTI; |
| 639 |
} |
| 640 |
else if (scanner.StringMatch("FastMonsters")) { |
| 641 |
skill.flags |= DSI_FAST_MONSTERS; |
| 642 |
} |
| 643 |
else if (scanner.StringMatch("InstantReaction")) { |
| 644 |
skill.flags |= DSI_INSTANT_REACTION; |
| 645 |
} |
| 646 |
else if (scanner.StringMatch("NoPain")) { |
| 647 |
skill.flags |= DSI_NO_PAIN; |
| 648 |
} |
| 649 |
else if (scanner.StringMatch("DefaultSkill")) { |
| 650 |
skill.flags |= DSI_DEFAULT_SKILL; |
| 651 |
} |
| 652 |
else if (scanner.StringMatch("PlayerRespawn")) { |
| 653 |
skill.flags |= DSI_PLAYER_RESPAWN; |
| 654 |
} |
| 655 |
else if (scanner.StringMatch("EasyBossBrain")) { |
| 656 |
skill.flags |= DSI_EASY_BOSS_BRAIN; |
| 657 |
} |
| 658 |
else { |
| 659 |
dsda_SkipValue(scanner); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
for (auto &old_skill : doom_mapinfo_skills) |
| 664 |
if (!stricmp(old_skill.unique_id, skill.unique_id)) { |
| 665 |
dsda_FreeSkill(old_skill); |
| 666 |
old_skill = skill; |
| 667 |
|
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
doom_mapinfo_skills.push_back(skill); |
| 672 |
} |
| 673 |
|
| 674 |
static void dsda_FreeCluster(doom_mapinfo_cluster_t &cluster) { |
| 675 |
Z_Free(cluster.enter_text); |
| 676 |
Z_Free(cluster.exit_text); |
| 677 |
Z_Free(cluster.music); |
| 678 |
Z_Free(cluster.flat); |
| 679 |
Z_Free(cluster.pic); |
| 680 |
} |
| 681 |
|
| 682 |
static void dsda_ParseDoomMapInfoCluster(Scanner &scanner) { |
| 683 |
doom_mapinfo_cluster_t cluster = { 0 }; |
| 684 |
|
| 685 |
scanner.MustGetInteger(); |
| 686 |
cluster.id = scanner.number; |
| 687 |
|
| 688 |
scanner.MustGetToken('{'); |
| 689 |
while (!scanner.CheckToken('}')) { |
| 690 |
scanner.MustGetToken(TK_Identifier); |
| 691 |
|
| 692 |
if (scanner.StringMatch("EnterText")) { |
| 693 |
dsda_ParseDoomMapInfoMultiString(scanner, cluster.enter_text); |
| 694 |
} |
| 695 |
else if (scanner.StringMatch("ExitText")) { |
| 696 |
dsda_ParseDoomMapInfoMultiString(scanner, cluster.exit_text); |
| 697 |
} |
| 698 |
else if (scanner.StringMatch("Music")) { |
| 699 |
SCAN_STRING(cluster.music); |
| 700 |
} |
| 701 |
else if (scanner.StringMatch("Flat")) { |
| 702 |
SCAN_STRING(cluster.flat); |
| 703 |
} |
| 704 |
else if (scanner.StringMatch("Pic")) { |
| 705 |
SCAN_STRING(cluster.pic); |
| 706 |
} |
| 707 |
else { |
| 708 |
dsda_SkipValue(scanner); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
for (auto &old_cluster : doom_mapinfo_clusters) |
| 713 |
if (old_cluster.id == cluster.id) { |
| 714 |
dsda_FreeCluster(old_cluster); |
| 715 |
old_cluster = cluster; |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
doom_mapinfo_clusters.push_back(cluster); |
| 720 |
} |
| 721 |
|
| 722 |
static void dsda_FreeEpisode(doom_mapinfo_episode_t &episode) { |
| 723 |
Z_Free(episode.map_lump); |
| 724 |
Z_Free(episode.name); |
| 725 |
Z_Free(episode.pic_name); |
| 726 |
} |
| 727 |
|
| 728 |
static void dsda_ParseDoomMapInfoEpisode(Scanner &scanner) { |
| 729 |
doom_mapinfo_episode_t episode = { 0 }; |
| 730 |
|
| 731 |
scanner.MustGetString(); |
| 732 |
STR_DUP(episode.map_lump); |
| 733 |
|
| 734 |
if (episode.map_lump[0] == '&') |
| 735 |
scanner.ErrorF("Invalid episode map lump %s", episode.map_lump); |
| 736 |
|
| 737 |
scanner.MustGetToken('{'); |
| 738 |
while (!scanner.CheckToken('}')) { |
| 739 |
scanner.MustGetToken(TK_Identifier); |
| 740 |
|
| 741 |
if (scanner.StringMatch("Name")) { |
| 742 |
SCAN_STRING(episode.name); |
| 743 |
} |
| 744 |
else if (scanner.StringMatch("PicName")) { |
| 745 |
SCAN_STRING(episode.pic_name); |
| 746 |
} |
| 747 |
else if (scanner.StringMatch("Key")) { |
| 748 |
scanner.MustGetToken('='); |
| 749 |
scanner.MustGetString(); |
| 750 |
episode.key = tolower(scanner.string[0]); |
| 751 |
} |
| 752 |
else { |
| 753 |
dsda_SkipValue(scanner); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
doom_mapinfo_episodes.push_back(episode); |
| 758 |
} |
| 759 |
|
| 760 |
static void dsda_ParseDoomMapInfoIdentifier(Scanner &scanner) { |
| 761 |
scanner.MustGetToken(TK_Identifier); |
| 762 |
|
| 763 |
if (scanner.StringMatch("map")) { |
| 764 |
dsda_ParseDoomMapInfoMap(scanner); |
| 765 |
} |
| 766 |
else if (scanner.StringMatch("defaultmap")) { |
| 767 |
dsda_ParseDoomMapInfoDefaultMap(scanner); |
| 768 |
} |
| 769 |
else if (scanner.StringMatch("adddefaultmap")) { |
| 770 |
dsda_ParseDoomMapInfoAddDefaultMap(scanner); |
| 771 |
} |
| 772 |
else if (scanner.StringMatch("cluster")) { |
| 773 |
dsda_ParseDoomMapInfoCluster(scanner); |
| 774 |
} |
| 775 |
else if (scanner.StringMatch("episode")) { |
| 776 |
dsda_ParseDoomMapInfoEpisode(scanner); |
| 777 |
} |
| 778 |
else if (scanner.StringMatch("clearepisodes")) { |
| 779 |
for (auto &episode : doom_mapinfo_episodes) |
| 780 |
dsda_FreeEpisode(episode); |
| 781 |
doom_mapinfo_episodes.clear(); |
| 782 |
doom_mapinfo.episodes_cleared = true; |
| 783 |
} |
| 784 |
else if (scanner.StringMatch("skill")) { |
| 785 |
dsda_ParseDoomMapInfoSkill(scanner); |
| 786 |
} |
| 787 |
else if (scanner.StringMatch("clearskills")) { |
| 788 |
for (auto &skill : doom_mapinfo_skills) |
| 789 |
dsda_FreeSkill(skill); |
| 790 |
doom_mapinfo_skills.clear(); |
| 791 |
doom_mapinfo.skills_cleared = true; |
| 792 |
} |
| 793 |
else { |
| 794 |
dsda_SkipValue(scanner); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
doom_mapinfo_t doom_mapinfo; |
| 799 |
|
| 800 |
void dsda_ParseDoomMapInfo(const unsigned char* buffer, size_t length, doom_mapinfo_errorfunc err) { |
| 801 |
Scanner scanner((const char*) buffer, length); |
| 802 |
|
| 803 |
scanner.SetErrorCallback(err); |
| 804 |
|
| 805 |
DO_ONCE |
| 806 |
dsda_InitDefaultMap(); |
| 807 |
END_ONCE |
| 808 |
|
| 809 |
while (scanner.TokensLeft()) |
| 810 |
dsda_ParseDoomMapInfoIdentifier(scanner); |
| 811 |
|
| 812 |
doom_mapinfo.num_maps = doom_mapinfo_maps.size(); |
| 813 |
if(doom_mapinfo.num_maps > 0) { |
| 814 |
doom_mapinfo.maps = &doom_mapinfo_maps[0]; |
| 815 |
} else { |
| 816 |
doom_mapinfo.maps = NULL; |
| 817 |
} |
| 818 |
|
| 819 |
doom_mapinfo.num_skills = doom_mapinfo_skills.size(); |
| 820 |
if(doom_mapinfo.num_skills > 0) { |
| 821 |
doom_mapinfo.skills = &doom_mapinfo_skills[0]; |
| 822 |
} else { |
| 823 |
doom_mapinfo.skills = NULL; |
| 824 |
} |
| 825 |
|
| 826 |
doom_mapinfo.num_clusters = doom_mapinfo_clusters.size(); |
| 827 |
if(doom_mapinfo.num_clusters > 0) { |
| 828 |
doom_mapinfo.clusters = &doom_mapinfo_clusters[0]; |
| 829 |
} else { |
| 830 |
doom_mapinfo.clusters = NULL; |
| 831 |
} |
| 832 |
|
| 833 |
doom_mapinfo.num_episodes = doom_mapinfo_episodes.size(); |
| 834 |
if(doom_mapinfo.num_episodes > 0) { |
| 835 |
doom_mapinfo.episodes = &doom_mapinfo_episodes[0]; |
| 836 |
} else { |
| 837 |
doom_mapinfo.episodes = NULL; |
| 838 |
} |
| 839 |
|
| 840 |
doom_mapinfo.loaded = true; |
| 841 |
} |