diff --git a/src/deh_lua.c b/src/deh_lua.c index c056db82a..64fb52fc7 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -59,24 +59,19 @@ static inline int lib_freeslot(lua_State *L) } else if (fastcmp(type, "SPR")) { - char wad; spritenum_t j; - lua_getfield(L, LUA_REGISTRYINDEX, "WAD"); - wad = (char)lua_tointeger(L, -1); - lua_pop(L, 1); + + if (strlen(word) > MAXSPRITENAME) + return luaL_error(L, "Sprite name is longer than %d characters\n", MAXSPRITENAME); + for (j = SPR_FIRSTFREESLOT; j <= SPR_LASTFREESLOT; j++) { - if (used_spr[(j-SPR_FIRSTFREESLOT)/8] & (1<<(j%8))) - { - if (!sprnames[j][4] && memcmp(sprnames[j],word,4)==0) - sprnames[j][4] = wad; + if (in_bit_array(used_spr, j - SPR_FIRSTFREESLOT)) continue; // Already allocated, next. - } // Found a free slot! CONS_Printf("Sprite SPR_%s allocated.\n",word); - strncpy(sprnames[j],word,4); - //sprnames[j][4] = 0; - used_spr[(j-SPR_FIRSTFREESLOT)/8] |= 1<<(j%8); // Okay, this sprite slot has been named now. + strcpy(sprnames[j], word); + set_bit_array(used_spr, j - SPR_FIRSTFREESLOT); // Okay, this sprite slot has been named now. // Lua needs to update the value in _G if it exists LUA_UpdateSprName(word, j); lua_pushinteger(L, j); @@ -455,17 +450,19 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word) } else if (fastncmp("SPR_",word,4)) { p = word+4; - for (i = 0; i < NUMSPRITES; i++) - if (!sprnames[i][4] && fastncmp(p,sprnames[i],4)) { - // updating overridden sprnames is not implemented for soc parser, - // so don't use cache - if (mathlib) - lua_pushinteger(L, i); - else - CacheAndPushConstant(L, word, i); - return 1; - } - if (mathlib) return luaL_error(L, "sprite '%s' could not be found.\n", word); + i = R_GetSpriteNumByName(p); + if (i != NUMSPRITES) + { + // updating overridden sprnames is not implemented for soc parser, + // so don't use cache + if (mathlib) + lua_pushinteger(L, i); + else + CacheAndPushConstant(L, word, i); + return 1; + } + else if (mathlib) + return luaL_error(L, "sprite '%s' could not be found.\n", word); return 0; } else if (fastncmp("SPR2_",word,5)) { @@ -738,18 +735,18 @@ static inline int lib_getenum(lua_State *L) // If a sprname has been "cached" to _G, update it to a new value. void LUA_UpdateSprName(const char *name, lua_Integer value) { - char fullname[9] = "SPR_XXXX"; + char fullname[4 + MAXSPRITENAME + 1] = "SPR_"; if (!gL) return; - strncpy(&fullname[4], name, 4); + strcpy(&fullname[4], name); lua_pushstring(gL, fullname); lua_rawget(gL, LUA_GLOBALSINDEX); if (!lua_isnil(gL, -1)) { - lua_pushstring(gL, name); + lua_pushstring(gL, fullname); lua_pushinteger(gL, value); lua_rawset(gL, LUA_GLOBALSINDEX); } diff --git a/src/deh_soc.c b/src/deh_soc.c index 65db63ebb..dda3b2ef4 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -405,7 +405,6 @@ void readfreeslots(MYFILE *f) { char *s = Z_Malloc(MAXLINELEN, PU_STATIC, NULL); char *word,*type; - char *tmp; int i; do @@ -415,10 +414,13 @@ void readfreeslots(MYFILE *f) if (s[0] == '\n') break; - tmp = strchr(s, '#'); - if (tmp) - *tmp = '\0'; - if (s == tmp) + char *hashtag = strchr(s, '#'); + char *space = strchr(s, ' '); + if (hashtag) + *hashtag = '\0'; + if (space) + *space = '\0'; + if (s == hashtag || s == space) continue; // Skip comment lines, but don't break. type = strtok(s, "_"); @@ -440,18 +442,16 @@ void readfreeslots(MYFILE *f) S_AddSoundFx(word, false, 0, false); else if (fastcmp(type, "SPR")) { + if (strlen(word) > MAXSPRITENAME) + I_Error("Sprite name is longer than %d characters\n", MAXSPRITENAME); + for (i = SPR_FIRSTFREESLOT; i <= SPR_LASTFREESLOT; i++) { - if (used_spr[(i-SPR_FIRSTFREESLOT)/8] & (1<<(i%8))) - { - if (!sprnames[i][4] && memcmp(sprnames[i],word,4)==0) - sprnames[i][4] = (char)f->wad; + if (in_bit_array(used_spr, i - SPR_FIRSTFREESLOT)) continue; // Already allocated, next. - } // Found a free slot! - strncpy(sprnames[i],word,4); - //sprnames[i][4] = 0; - used_spr[(i-SPR_FIRSTFREESLOT)/8] |= 1<<(i%8); // Okay, this sprite slot has been named now. + strcpy(sprnames[i], word); + set_bit_array(used_spr, i - SPR_FIRSTFREESLOT); // Okay, this sprite slot has been named now. // Lua needs to update the value in _G if it exists LUA_UpdateSprName(word, i); break; @@ -4182,9 +4182,9 @@ spritenum_t get_sprite(const char *word) return atoi(word); if (fastncmp("SPR_",word,4)) word += 4; // take off the SPR_ - for (i = 0; i < NUMSPRITES; i++) - if (!sprnames[i][4] && memcmp(word,sprnames[i],4)==0) - return i; + i = R_GetSpriteNumByName(word); + if (i != NUMSPRITES) + return i; deh_warning("Couldn't find sprite named 'SPR_%s'",word); return SPR_NULL; } diff --git a/src/deh_tables.c b/src/deh_tables.c index ed401d68a..c7c7c6040 100644 --- a/src/deh_tables.c +++ b/src/deh_tables.c @@ -31,7 +31,7 @@ char *FREE_STATES[NUMSTATEFREESLOTS]; char *FREE_MOBJS[NUMMOBJFREESLOTS]; char *FREE_SKINCOLORS[NUMCOLORFREESLOTS]; -UINT8 used_spr[(NUMSPRITEFREESLOTS / 8) + 1]; // Bitwise flag for sprite freeslot in use! I would use ceil() here if I could, but it only saves 1 byte of memory anyway. +bitarray_t used_spr[BIT_ARRAY_SIZE(NUMSPRITEFREESLOTS)]; // Sprite freeslots in use const char NIGHTSGRADE_LIST[] = { 'F', // GRADE_F diff --git a/src/deh_tables.h b/src/deh_tables.h index 42716f9b4..b6986adff 100644 --- a/src/deh_tables.h +++ b/src/deh_tables.h @@ -23,13 +23,13 @@ extern char *FREE_STATES[NUMSTATEFREESLOTS]; extern char *FREE_MOBJS[NUMMOBJFREESLOTS]; extern char *FREE_SKINCOLORS[NUMCOLORFREESLOTS]; -extern UINT8 used_spr[(NUMSPRITEFREESLOTS / 8) + 1]; // Bitwise flag for sprite freeslot in use! I would use ceil() here if I could, but it only saves 1 byte of memory anyway. +extern bitarray_t used_spr[BIT_ARRAY_SIZE(NUMSPRITEFREESLOTS)]; // Sprite freeslots in use #define initfreeslots() {\ - memset(FREE_STATES,0,sizeof(char *) * NUMSTATEFREESLOTS);\ - memset(FREE_MOBJS,0,sizeof(char *) * NUMMOBJFREESLOTS);\ - memset(FREE_SKINCOLORS,0,sizeof(char *) * NUMCOLORFREESLOTS);\ - memset(used_spr,0,sizeof(UINT8) * ((NUMSPRITEFREESLOTS / 8) + 1));\ + memset(FREE_STATES, 0, sizeof(FREE_STATES));\ + memset(FREE_MOBJS, 0, sizeof(FREE_MOBJS));\ + memset(FREE_SKINCOLORS, 0, sizeof(FREE_SKINCOLORS));\ + memset(used_spr, 0, sizeof(used_spr));\ memset(actionsoverridden, LUA_REFNIL, sizeof(actionsoverridden));\ } diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index 656fbc4a6..0bb8de851 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -571,19 +571,15 @@ void HWR_LoadModels(void) } // Add sprite models. - // Must be 4 characters long exactly. Otherwise, it's not a sprite name. - if (len == 4) + for (i = 0; i < numsprites; i++) { - for (i = 0; i < numsprites; i++) + if (stricmp(name, sprnames[i]) == 0) { - if (stricmp(name, sprnames[i]) == 0) - { - md2_models[i].scale = scale; - md2_models[i].offset = offset; - md2_models[i].found = true; - strcpy(md2_models[i].filename, filename); - goto modelfound; - } + md2_models[i].scale = scale; + md2_models[i].offset = offset; + md2_models[i].found = true; + strcpy(md2_models[i].filename, filename); + goto modelfound; } } diff --git a/src/info.c b/src/info.c index ab46cdbc7..9b33a57ab 100644 --- a/src/info.c +++ b/src/info.c @@ -26,9 +26,10 @@ #include "hardware/hw_light.h" #endif + // Hey, moron! If you change this table, don't forget about the sprite enum in info.h and the sprite lights in hw_light.c! // For the sake of constant merge conflicts, let's spread this out -char sprnames[NUMSPRITES + 1][5] = +char sprnames[NUMSPRITES + 1][MAXSPRITENAME + 1] = { "NULL", // invisible object "UNKN", @@ -525,7 +526,7 @@ char sprnames[NUMSPRITES + 1][5] = "GWLR", }; -char spr2names[NUMPLAYERSPRITES][5] = +char spr2names[NUMPLAYERSPRITES][MAXSPRITENAME + 1] = { "STND", "WAIT", diff --git a/src/info.h b/src/info.h index 9475b2302..0361f6428 100644 --- a/src/info.h +++ b/src/info.h @@ -575,6 +575,7 @@ extern int actionsoverridden[NUMACTIONS][MAX_ACTION_RECURSION]; #define NUMMOBJFREESLOTS 1024 #define NUMSPRITEFREESLOTS NUMMOBJFREESLOTS #define NUMSTATEFREESLOTS (NUMMOBJFREESLOTS*8) +#define MAXSPRITENAME 64 // Hey, moron! If you change this table, don't forget about sprnames in info.c and the sprite lights in hw_light.c! typedef enum sprite @@ -4383,8 +4384,8 @@ typedef struct } state_t; extern state_t states[NUMSTATES]; -extern char sprnames[NUMSPRITES + 1][5]; -extern char spr2names[NUMPLAYERSPRITES][5]; +extern char sprnames[NUMSPRITES + 1][MAXSPRITENAME + 1]; +extern char spr2names[NUMPLAYERSPRITES][MAXSPRITENAME + 1]; extern playersprite_t spr2defaults[NUMPLAYERSPRITES]; extern state_t *astate; extern playersprite_t free_spr2; diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 9e4eebeb0..f6b8f462b 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -3030,6 +3030,9 @@ static int lib_rFrame2Char(lua_State *L) //HUDSAFE c[0] = R_Frame2Char(ch); + if (c[0] == '\xFF') + return luaL_error(L, "frame %u cannot be represented by a character", ch); + c[1] = 0; lua_pushstring(L, c); diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index ddd6c6888..d6771f108 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -492,9 +492,7 @@ static int libd_getSpritePatch(lua_State *L) else if (lua_isstring(L, 1)) // sprite prefix name given, e.g. "THOK" { const char *name = lua_tostring(L, 1); - for (i = 0; i < NUMSPRITES; i++) - if (fastcmp(name, sprnames[i])) - break; + i = R_GetSpriteNumByName(name); if (i >= NUMSPRITES) return 0; } diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 0bcbcc5b9..eeb1067a3 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -88,12 +88,12 @@ static int lib_getSprname(lua_State *L) else if (lua_isstring(L, 1)) { const char *name = lua_tostring(L, 1); - for (i = 0; i < NUMSPRITES; i++) - if (fastcmp(name, sprnames[i])) - { - lua_pushinteger(L, i); - return 1; - } + i = R_GetSpriteNumByName(name); + if (i != NUMSPRITES) + { + lua_pushinteger(L, i); + return 1; + } } return 0; } @@ -245,22 +245,15 @@ static int lib_getSpriteInfo(lua_State *L) if (lua_isstring(L, 1)) { const char *name = lua_tostring(L, 1); - INT32 spr; - for (spr = 0; spr < NUMSPRITES; spr++) - { - if (fastcmp(name, sprnames[spr])) - { - i = spr; - break; - } - } - if (i == NUMSPRITES) + INT32 spr = R_GetSpriteNumByName(name); + if (spr == NUMSPRITES) { char *check; i = strtol(name, &check, 10); if (check == name || *check != '\0') return luaL_error(L, "unknown sprite name %s", name); } + i = spr; } else i = luaL_checkinteger(L, 1); @@ -359,8 +352,8 @@ static int PopPivotTable(spriteinfo_t *info, lua_State *L, int stk) default: TYPEERROR("pivot frame", LUA_TNUMBER, lua_type(L, stk+1)); } - if ((idx < 0) || (idx >= 64)) - return luaL_error(L, "pivot frame %d out of range (0 - %d)", idx, 63); + if ((idx < 0) || (idx >= MAXFRAMENUM)) + return luaL_error(L, "pivot frame %d out of range (0 - %d)", idx, MAXFRAMENUM - 1); // the values in pivot[] are also tables if (PopPivotSubTable(info->pivot, L, stk+2, idx)) info->available = true; @@ -555,7 +548,7 @@ static int pivotlist_set(lua_State *L) static int pivotlist_num(lua_State *L) { - lua_pushinteger(L, 64); + lua_pushinteger(L, MAXFRAMENUM); return 1; } diff --git a/src/lua_script.c b/src/lua_script.c index b62fa675e..057899555 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -622,9 +622,6 @@ static inline boolean LUA_LoadFile(MYFILE *f, char *name) if (!gL) // Lua needs to be initialized LUA_ClearState(); - lua_pushinteger(gL, f->wad); - lua_setfield(gL, LUA_REGISTRYINDEX, "WAD"); - lua_pushcfunction(gL, LUA_GetErrorMessage); errorhandlerindex = lua_gettop(gL); diff --git a/src/p_pspr.h b/src/p_pspr.h index be0d9f39e..5fb676763 100644 --- a/src/p_pspr.h +++ b/src/p_pspr.h @@ -35,7 +35,7 @@ #pragma interface #endif -/// \brief Frame flags: only the frame number - 0 to 256 (Frames from 0 to 63, Sprite2 number uses 0 to 127 plus FF_SPR2SUPER) +/// \brief Frame flags: only the frame number - 0 to 256 (Frames from 0 to 255, Sprite2 number uses 0 to 127 plus FF_SPR2SUPER) #define FF_FRAMEMASK 0xff /// \brief Frame flags - SPR2: Super sprite2 diff --git a/src/r_defs.h b/src/r_defs.h index cb94dd6e5..da4dd2d70 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -976,6 +976,8 @@ typedef struct #endif } spriteframe_t; +#define MAXFRAMENUM 256 + // // A sprite definition: a number of animation frames. // diff --git a/src/r_picformats.c b/src/r_picformats.c index e4a59f211..d71657021 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -1570,7 +1570,7 @@ static void R_ParseSpriteInfo(boolean spr2) spriteinfo_t *info; char *sprinfoToken; size_t sprinfoTokenLength; - char newSpriteName[5]; // no longer dynamically allocated + char newSpriteName[MAXSPRITENAME + 1]; // no longer dynamically allocated spritenum_t sprnum = NUMSPRITES; playersprite_t spr2num = NUMPLAYERSPRITES; INT32 i; @@ -1584,31 +1584,17 @@ static void R_ParseSpriteInfo(boolean spr2) I_Error("Error parsing SPRTINFO lump: Unexpected end of file where sprite name should be"); } sprinfoTokenLength = strlen(sprinfoToken); - if (sprinfoTokenLength != 4) - { - I_Error("Error parsing SPRTINFO lump: Sprite name \"%s\" isn't 4 characters long",sprinfoToken); - } - else - { - memset(&newSpriteName, 0, 5); - M_Memcpy(newSpriteName, sprinfoToken, sprinfoTokenLength); - // ^^ we've confirmed that the token is == 4 characters so it will never overflow a 5 byte char buffer - strupr(newSpriteName); // Just do this now so we don't have to worry about it - } + if (sprinfoTokenLength > MAXSPRITENAME) + I_Error("Error parsing SPRTINFO lump: Sprite name \"%s\" is longer than %d characters", sprinfoToken, MAXSPRITENAME); + strcpy(newSpriteName, sprinfoToken); + strupr(newSpriteName); // Just do this now so we don't have to worry about it Z_Free(sprinfoToken); if (!spr2) { - for (i = 0; i <= NUMSPRITES; i++) - { - if (i == NUMSPRITES) - I_Error("Error parsing SPRTINFO lump: Unknown sprite name \"%s\"", newSpriteName); - if (!memcmp(newSpriteName,sprnames[i],4)) - { - sprnum = i; - break; - } - } + sprnum = R_GetSpriteNumByName(newSpriteName); + if (sprnum == NUMSPRITES) + I_Error("Error parsing SPRTINFO lump: Unknown sprite name \"%s\"", newSpriteName); } else { diff --git a/src/r_picformats.h b/src/r_picformats.h index 3ee9805d8..098f927a5 100644 --- a/src/r_picformats.h +++ b/src/r_picformats.h @@ -100,7 +100,7 @@ typedef struct typedef struct { - spriteframepivot_t pivot[64]; + spriteframepivot_t pivot[MAXFRAMENUM]; boolean available; } spriteinfo_t; diff --git a/src/r_skins.c b/src/r_skins.c index 29a1556c0..d0d2eed62 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -625,7 +625,7 @@ static void R_LoadSkinSprites(UINT16 wadnum, UINT16 *lump, UINT16 *lastlump, ski newlastlump++; // load all sprite sets we are aware of... for super! for (sprite2 = start_spr2; sprite2 < free_spr2; sprite2++) - R_AddSingleSpriteDef(spr2names[sprite2], &skin->super.sprites[sprite2], wadnum, newlastlump, *lastlump); + R_AddSingleSpriteDef(spr2names[sprite2], &skin->super.sprites[sprite2], wadnum, newlastlump, *lastlump, false); newlastlump--; *lastlump = newlastlump; // okay, make the normal sprite set loading end there @@ -633,7 +633,7 @@ static void R_LoadSkinSprites(UINT16 wadnum, UINT16 *lump, UINT16 *lastlump, ski // load all sprite sets we are aware of... for normal stuff. for (sprite2 = start_spr2; sprite2 < free_spr2; sprite2++) - R_AddSingleSpriteDef(spr2names[sprite2], &skin->sprites[sprite2], wadnum, *lump, *lastlump); + R_AddSingleSpriteDef(spr2names[sprite2], &skin->sprites[sprite2], wadnum, *lump, *lastlump, false); if (skin->sprites[0].numframes == 0) CONS_Alert(CONS_ERROR, M_GetText("No frames found for sprite SPR2_%s\n"), spr2names[0]); diff --git a/src/r_things.c b/src/r_things.c index a5e9fbb5f..41e1c144e 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -77,7 +77,7 @@ spriteinfo_t spriteinfo[NUMSPRITES]; spritedef_t *sprites; size_t numsprites; -static spriteframe_t sprtemp[64]; +static spriteframe_t sprtemp[MAXFRAMENUM]; static size_t maxframe; static const char *spritename; @@ -116,6 +116,14 @@ static INT32 drawsegs_xrange_count = 0; // // ========================================================================== +spritenum_t R_GetSpriteNumByName(const char *name) +{ + for (spritenum_t i = 0; i < NUMSPRITES; i++) + if (!strcmp(name, sprnames[i])) + return i; + return NUMSPRITES; +} + // // // @@ -128,10 +136,14 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch { char cn = R_Frame2Char(frame), cr = R_Rotation2Char(rotation); // for debugging + char framedescription[256]; + if (cn != '\xFF') + sprintf(framedescription, "%s frame %d (%c)", spritename, frame, cn); + else + sprintf(framedescription, "%s frame %d", spritename, frame); + INT32 r; - lumpnum_t lumppat = wad; - lumppat <<= 16; - lumppat += lump; + lumpnum_t lumppat = (wad << 16) + lump; if (maxframe ==(size_t)-1 || frame > maxframe) maxframe = frame; @@ -147,9 +159,9 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch { // the lump should be used for all rotations if (sprtemp[frame].rotate == SRF_SINGLE) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has multiple rot = 0 lump\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has multiple rot = 0 lump\n", framedescription); else if (sprtemp[frame].rotate != SRF_NONE) // Let's bundle 1-8/16 and L/R rotations into one debug message. - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has rotations and a rot = 0 lump\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has rotations and a rot = 0 lump\n", framedescription); sprtemp[frame].rotate = SRF_SINGLE; for (r = 0; r < 16; r++) @@ -169,15 +181,15 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch if (sprtemp[frame].rotate == SRF_NONE) sprtemp[frame].rotate = SRF_SINGLE; else if (sprtemp[frame].rotate == SRF_SINGLE) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has L/R rotations and a rot = 0 lump\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has L/R rotations and a rot = 0 lump\n", framedescription); else if (sprtemp[frame].rotate == SRF_3D) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has both L/R and 1-8 rotations\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has both L/R and 1-8 rotations\n", framedescription); else if (sprtemp[frame].rotate == SRF_3DGE) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has both L/R and 1-G rotations\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has both L/R and 1-G rotations\n", framedescription); else if ((sprtemp[frame].rotate & SRF_LEFT) && (rotation == ROT_L)) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has multiple L rotations\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has multiple L rotations\n", framedescription); else if ((sprtemp[frame].rotate & SRF_RIGHT) && (rotation == ROT_R)) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has multiple R rotations\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has multiple R rotations\n", framedescription); sprtemp[frame].rotate |= ((rotation == ROT_R) ? SRF_RIGHT : SRF_LEFT); if ((sprtemp[frame].rotate & SRF_2D) == SRF_2D) @@ -204,9 +216,9 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch if (sprtemp[frame].rotate == SRF_NONE) sprtemp[frame].rotate = SRF_SINGLE; else if (sprtemp[frame].rotate == SRF_SINGLE) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has 1-8/G rotations and a rot = 0 lump\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has 1-8/G rotations and a rot = 0 lump\n", framedescription); else if (sprtemp[frame].rotate & SRF_2D) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s frame %c has both L/R and 1-8/G rotations\n", spritename, cn); + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s has both L/R and 1-8/G rotations\n", framedescription); // make 0 based rotation--; @@ -226,7 +238,12 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch } if (sprtemp[frame].lumppat[rotation] != LUMPERROR) - CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s: %c%c has two lumps mapped to it\n", spritename, cn, cr); + { + if (cn != '\xFF') + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s: %d_%c (%c%c) has two lumps mapped to it\n", spritename, frame, cr, cn, cr); + else + CONS_Debug(DBG_SETUP, "R_InitSprites: Sprite %s: %d_%c has two lumps mapped to it\n", spritename, frame, cr); + } // lumppat & lumpid are the same for original Doom, but different // when using sprites in pwad : the lumppat points the new graphics @@ -238,24 +255,201 @@ static void R_InstallSpriteLump(UINT16 wad, // graphics patch sprtemp[frame].flip &= ~(1<= 64 || *ret_rotation == 255) + return false; + + if (namelen == 8) + { + *ret_frame2 = R_Char2Frame(name[6]); + *ret_rotation2 = R_Char2Rotation(name[7]); + if (*ret_frame2 >= 64 || *ret_rotation2 == 255) + return false; + } + else + { + *ret_frame2 = -1; + *ret_rotation2 = 255; + } + + return true; +} + +static boolean GetSingleFrameAndRotation( + const char *name, + size_t len, + INT32 *ret_frame, + UINT8 *ret_rotation +) +{ + const char *underscore = strchr(name, '_'); + + // Found but past the part of the name we are parsing + if ((size_t)(underscore - name) >= len) + underscore = NULL; + + size_t framelen = underscore ? (size_t)(underscore - name) : len; + if (framelen < 1 || framelen > 4) + return false; + + char framepart[4 + 1]; // Max 9999 + strlcpy(framepart, name, framelen + 1); + + for (size_t i = 0; i < framelen; i++) + if (!isdigit(framepart[i])) + return false; + + *ret_frame = atoi(framepart); + *ret_rotation = underscore ? R_Char2Rotation(*(underscore + 1)) : 0; + if (*ret_frame >= MAXFRAMENUM || *ret_rotation == 255) + return false; + + return true; +} + +static boolean GetFramesAndRotationsFromLongLumpName( + const char *name, + INT32 *ret_frame, + UINT8 *ret_rotation, + INT32 *ret_frame2, + UINT8 *ret_rotation2 +) +{ + const char *plus = strchr(name, '+'); + + if (plus) + { + size_t len1 = plus - name; + + if (!GetSingleFrameAndRotation(name, len1, ret_frame, ret_rotation)) + return false; + if (!GetSingleFrameAndRotation(plus + 1, strlen(name) - len1 - 1, ret_frame2, ret_rotation2)) + return false; + } + else + { + if (!GetSingleFrameAndRotation(name, strlen(name), ret_frame, ret_rotation)) + return false; + + *ret_frame2 = -1; + *ret_rotation2 = 255; + } + + return true; +} + +static UINT8 GetOppositeRotation(UINT8 rotation, UINT8 flags) +{ + if (flags & ~SRF_3DMASK) + I_Error("GetOppositeRotation: rotation type not supported"); + + UINT8 numrotations = (flags == SRF_3D) ? 8 : 16; + return (rotation == 1) ? 1 : numrotations + 2 - rotation; +} + +static void MirrorMissingRotations(void) +{ + for (UINT32 framenum = 0; framenum < maxframe; framenum++) + { + spriteframe_t *frame = &sprtemp[framenum]; + + if (frame->rotate == SRF_NONE || !(frame->rotate & SRF_3DMASK)) + continue; + + UINT8 numrotations = frame->rotate == SRF_3D ? 8 : 16; + + for (UINT8 rotation = 1; rotation <= numrotations; rotation++) + { + if (frame->lumppat[rotation - 1] != LUMPERROR) + continue; + + UINT8 baserotation = GetOppositeRotation(rotation, frame->rotate); + UINT32 lumpnum = frame->lumppat[baserotation - 1]; + R_InstallSpriteLump(WADFILENUM(lumpnum), LUMPNUM(lumpnum), frame->lumpid[baserotation], framenum, rotation, 1); + } + } +} + +// Some checks to help development +static void CheckFrame(const char *sprname) +{ + for (UINT32 frame = 0; frame < maxframe; frame++) + { + spriteframe_t *spriteframe = &sprtemp[frame]; + + char framedescription[256]; + if (frame < 64) + sprintf(framedescription, "%s frame %d (%c)", sprname, frame, R_Frame2Char(frame)); + else + sprintf(framedescription, "%s frame %d", sprname, frame); + + switch (spriteframe->rotate) + { + case SRF_NONE: + // no rotations were found for that frame at all + I_Error("R_AddSingleSpriteDef: No patches found for %s", framedescription); + break; + + case SRF_SINGLE: + // only the first rotation is needed + break; + + case SRF_2D: // both Left and Right rotations + // we test to see whether the left and right slots are present + if ((spriteframe->lumppat[2] == LUMPERROR) || (spriteframe->lumppat[6] == LUMPERROR)) + I_Error("R_AddSingleSpriteDef: Sprite %s is missing rotations (L-R mode)", + framedescription); + break; + + default: + { + // must have all 8/16 frames + UINT8 rotation = ((spriteframe->rotate & SRF_3DGE) ? 16 : 8); + while (rotation--) + { + // we test the patch lump, or the id lump whatever + // if it was not loaded the two are LUMPERROR + if (spriteframe->lumppat[rotation] == LUMPERROR) + I_Error("R_AddSingleSpriteDef: Sprite %s is missing rotations (1-%c mode)", + framedescription, ((spriteframe->rotate & SRF_3DGE) ? 'G' : '8')); + } + } + break; + } + } +} + // Install a single sprite, given its identifying name (4 chars) // // (originally part of R_AddSpriteDefs) // -// Pass: name of sprite : 4 chars +// Pass: name of sprite // spritedef_t // wadnum : wad number, indexes wadfiles[], where patches // for frames are found // startlump : first lump to search for sprite frames // endlump : AFTER the last lump to search +// longname : whether to use long sprite names or 4-char names // // Returns true if the sprite was succesfully added // -boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 wadnum, UINT16 startlump, UINT16 endlump) +boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 wadnum, UINT16 startlump, UINT16 endlump, boolean longname) { UINT16 l; - UINT8 frame; - UINT8 rotation; lumpinfo_t *lumpinfo; UINT16 numadded = 0; @@ -282,15 +476,23 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 for (l = startlump; l < endlump; l++) { - if (memcmp(lumpinfo[l].name,sprname,4)==0) + if (longname && W_IsLumpFolder(wadnum, l)) + I_Error("R_AddSingleSpriteDef: all frame lumps for a sprite should be contained inside a single folder\n"); + + // For long sprites, the startlump-endlump range only includes + // relevant lumps, so no check needed in that case + if (longname || (strlen(sprname) == 4 && !memcmp(lumpinfo[l].name, sprname, 4))) { INT16 width, height; INT16 topoffset, leftoffset; + INT32 frame, frame2; + UINT8 rotation, rotation2; - frame = R_Char2Frame(lumpinfo[l].name[4]); - rotation = R_Char2Rotation(lumpinfo[l].name[5]); + boolean good = longname ? + GetFramesAndRotationsFromLongLumpName(lumpinfo[l].longname, &frame, &rotation, &frame2, &rotation2) : + GetFramesAndRotationsFromShortLumpName(lumpinfo[l].name, &frame, &rotation, &frame2, &rotation2); - if (frame >= 64 || rotation == 255) // Give an actual NAME error -_-... + if (!good) // Give an actual NAME error -_-... { CONS_Alert(CONS_WARNING, M_GetText("Bad sprite name: %s\n"), W_CheckNameForNumPwad(wadnum,l)); continue; @@ -322,19 +524,8 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 //---------------------------------------------------- R_InstallSpriteLump(wadnum, l, numspritelumps, frame, rotation, 0); - - if (lumpinfo[l].name[6]) - { - frame = R_Char2Frame(lumpinfo[l].name[6]); - rotation = R_Char2Rotation(lumpinfo[l].name[7]); - - if (frame >= 64 || rotation == 255) // Give an actual NAME error -_-... - { - CONS_Alert(CONS_WARNING, M_GetText("Bad sprite name: %s\n"), W_CheckNameForNumPwad(wadnum,l)); - continue; - } - R_InstallSpriteLump(wadnum, l, numspritelumps, frame, rotation, 1); - } + if (frame2 != -1) + R_InstallSpriteLump(wadnum, l, numspritelumps, frame2, rotation2, 1); if (++numspritelumps >= max_spritelumps) { @@ -374,41 +565,10 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 maxframe++; - // - // some checks to help development - // - for (frame = 0; frame < maxframe; frame++) - { - switch (sprtemp[frame].rotate) - { - case SRF_NONE: - // no rotations were found for that frame at all - I_Error("R_AddSingleSpriteDef: No patches found for %.4s frame %c", sprname, R_Frame2Char(frame)); - break; + if (longname) + MirrorMissingRotations(); - case SRF_SINGLE: - // only the first rotation is needed - break; - - case SRF_2D: // both Left and Right rotations - // we test to see whether the left and right slots are present - if ((sprtemp[frame].lumppat[2] == LUMPERROR) || (sprtemp[frame].lumppat[6] == LUMPERROR)) - I_Error("R_AddSingleSpriteDef: Sprite %.4s frame %c is missing rotations (L-R mode)", - sprname, R_Frame2Char(frame)); - break; - - default: - // must have all 8/16 frames - rotation = ((sprtemp[frame].rotate & SRF_3DGE) ? 16 : 8); - while (rotation--) - // we test the patch lump, or the id lump whatever - // if it was not loaded the two are LUMPERROR - if (sprtemp[frame].lumppat[rotation] == LUMPERROR) - I_Error("R_AddSingleSpriteDef: Sprite %.4s frame %c is missing rotations (1-%c mode)", - sprname, R_Frame2Char(frame), ((sprtemp[frame].rotate & SRF_3DGE) ? 'G' : '8')); - break; - } - } + CheckFrame(sprname); // allocate space for the frames present and copy sprtemp to it if (spritedef->numframes && // has been allocated @@ -429,14 +589,10 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 return true; } -// -// Search for sprites replacements in a wad whose names are in namelist -// -void R_AddSpriteDefs(UINT16 wadnum) +static void AddShortSpriteDefs(UINT16 wadnum, size_t *ptr_spritesadded, size_t *ptr_framesadded) { - size_t i, addsprites = 0; + size_t i; UINT16 start, end; - char wadname[MAX_WADPATH]; // Find the sprites section in this resource file. switch (wadfiles[wadnum]->type) @@ -474,27 +630,90 @@ void R_AddSpriteDefs(UINT16 wadnum) return; } - // // scan through lumps, for each sprite, find all the sprite frames // for (i = 0; i < numsprites; i++) { - if (sprnames[i][4] && wadnum >= (UINT16)sprnames[i][4]) - continue; - - if (R_AddSingleSpriteDef(sprnames[i], &sprites[i], wadnum, start, end)) + if (R_AddSingleSpriteDef(sprnames[i], &sprites[i], wadnum, start, end, false)) { // if a new sprite was added (not just replaced) - addsprites++; + (*ptr_spritesadded)++; #ifndef ZDEBUG CONS_Debug(DBG_SETUP, "sprite %s set in pwad %d\n", sprnames[i], wadnum); #endif } } - nameonly(strcpy(wadname, wadfiles[wadnum]->filename)); - CONS_Printf(M_GetText("%s added %d frames in %s sprites\n"), wadname, end-start, sizeu1(addsprites)); + *ptr_framesadded += end - start; +} + +static void AddLongSpriteDefs(UINT16 wadnum, size_t *ptr_spritesadded, size_t *ptr_framesadded) +{ + if (!W_FileHasFolders(wadfiles[wadnum])) + return; + + UINT16 start = W_CheckNumForFolderStartPK3("LongSprites/", wadnum, 0); + UINT16 end = W_CheckNumForFolderEndPK3("LongSprites/", wadnum, start); + + if (start == INT16_MAX || end == INT16_MAX || start >= end) + return; + + size_t lumpnum = start; + + while (lumpnum < end) + { + if (W_IsLumpFolder(wadnum, lumpnum)) + { + lumpnum++; + continue; + } + + UINT16 folderstart, folderend; + char *folderpath = W_GetLumpFolderPathPK3(wadnum, lumpnum); + folderstart = lumpnum; + folderend = W_CheckNumForFolderEndPK3(folderpath, wadnum, lumpnum); + Z_Free(folderpath); + + spritenum_t sprnum; + char *sprname = W_GetLumpFolderNamePK3(wadnum, lumpnum); + strupr(sprname); + sprnum = R_GetSpriteNumByName(sprname); + + if (sprnum != NUMSPRITES && R_AddSingleSpriteDef(sprname, &sprites[sprnum], wadnum, folderstart, folderend, true)) + { + // A new sprite was added (not just replaced) + (*ptr_spritesadded)++; +#ifndef ZDEBUG + CONS_Debug(DBG_SETUP, "long sprite %s set in pwad %d\n", sprname, wadnum); +#endif + } + + Z_Free(sprname); + + lumpnum = folderend; + } + + *ptr_framesadded += end - start; +} + +// +// Search for sprites replacements in a wad whose names are in namelist +// +void R_AddSpriteDefs(UINT16 wadnum) +{ + char wadname[MAX_WADPATH]; + size_t spritesadded = 0; + size_t framesadded = 0; + + AddShortSpriteDefs(wadnum, &spritesadded, &framesadded); + AddLongSpriteDefs(wadnum, &spritesadded, &framesadded); + + if (spritesadded || framesadded) + { + nameonly(strcpy(wadname, wadfiles[wadnum]->filename)); + CONS_Printf(M_GetText("%s added %s frames in %s sprites\n"), wadname, sizeu1(framesadded), sizeu2(spritesadded)); + } } // diff --git a/src/r_things.h b/src/r_things.h index 19bf7e34f..530898917 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -27,7 +27,9 @@ #define FEETADJUST (4<lumpinfo[lump].fullname; + + const char *slash = strrchr(fullname, '/'); + INT32 pathlen = slash ? slash - fullname : 0; + + char *path = Z_Calloc(pathlen + 1, PU_STATIC, NULL); + strncpy(path, fullname, pathlen); + + return path; +} + +char *W_GetLumpFolderNamePK3(UINT16 wad, UINT16 lump) +{ + const char *fullname = wadfiles[wad]->lumpinfo[lump].fullname; + size_t start, end; + + INT32 i = strlen(fullname); + + i--; + while (i >= 0 && fullname[i] != '/') + i--; + if (i < 0) + return NULL; + end = i; + + i--; + while (i >= 0 && fullname[i] != '/') + i--; + if (i < 0) + return NULL; + start = i + 1; + + size_t namelen = end - start; + char *foldername = Z_Calloc(namelen + 1, PU_STATIC, NULL); + strncpy(foldername, fullname + start, namelen); + + return foldername; +} + void W_GetFolderLumpsPwad(const char *name, UINT16 wad, UINT32 **list, UINT16 *list_capacity, UINT16 *numlumps) { size_t name_length = strlen(name); diff --git a/src/w_wad.h b/src/w_wad.h index e043e4d62..80e0e32fd 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -180,6 +180,8 @@ UINT16 W_CheckNumForMarkerStartPwad(const char *name, UINT16 wad, UINT16 startlu UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); +char *W_GetLumpFolderPathPK3(UINT16 wad, UINT16 lump); +char *W_GetLumpFolderNamePK3(UINT16 wad, UINT16 lump); void W_GetFolderLumpsPwad(const char *name, UINT16 wad, UINT32 **list, UINT16 *list_capacity, UINT16 *numlumps); void W_GetFolderLumps(const char *name, UINT32 **list, UINT16 *list_capacity, UINT16 *numlumps);