#include "ESPReader.h"

#define LE(x) (((x >> 24) & 0x000000ffu) | ((x >>  8) & 0x0000ff00u) | \
               ((x <<  8) & 0x00ff0000u) | ((x << 24) & 0xff000000u))


const enum record_type group_order[GO_SIZE] = {
	GMST, KYWD, LCRT, AACT, TXST, GLOB,
	CLAS, FACT, HDPT, HAIR, EYES, RACE,
	SOUN, ASPC, MGEF, SCPT, LTEX, ENCH,
	SPEL, SCRL, ACTI, TACT, ARMO, BOOK,
	CONT, DOOR, INGR, LIGH, MISC, APPA,
	STAT, SCOL, MSTT, PWAT, GRAS, TREE,
	CLDC, FLOR, FURN, WEAP, AMMO, NPC_,
	LVLN, KEYM, ALCH, IDLM, COBJ, PROJ,
	HAZD, SLGM, LVLI, WTHR, CLMT, SPGD,
	RFCT, REGN, NAVI, CELL, WRLD, DIAL,
	QUST, IDLE, PACK, CSTY, LSCR, LVSP,
	ANIO, WATR, EFSH, EXPL, DEBR, IMGS,
	IMAD, FLST, PERK, BPTD, ADDN, AVIF,
	CAMS, CPTH, VTYP, MATT, IPCT, IPDS,
	ARMA, ECZN, LCTN, MESG, RGDL, DOBJ,
	LGTM, MUSC, FSTP, FSTS, SMBN, SMQN,
	SMEN, DLBR, MUST, DLVW, WOOP, SHOU,
	EQUP, RELA, SCEN, ASTP, OTFT, ARTO,
	MATO, MOVT, HAZD, SNDR, DUAL, SNCT,
	SOPM, COLL, CLFM, REVB
};

// record type to record type value
const uint32_t rt[RT_SIZE] = {
	[AACT] = LE('AACT'), [ACHR] = LE('ACHR'), [ACTI] = LE('ACTI'),
	[ADDN] = LE('ADDN'), [ALCH] = LE('ALCH'), [AMMO] = LE('AMMO'),
	[ANIO] = LE('ANIO'), [APPA] = LE('APPA'), [ARMA] = LE('ARMA'),
	[ARMO] = LE('ARMO'), [ARTO] = LE('ARTO'), [ASPC] = LE('ASPC'),
	[ASTP] = LE('ASTP'), [AVIF] = LE('AVIF'), [BOOK] = LE('BOOK'),
	[BPTD] = LE('BPTD'), [CAMS] = LE('CAMS'), [CELL] = LE('CELL'),
	[CLAS] = LE('CLAS'), [CLDC] = LE('CLDC'), [CLFM] = LE('CLFM'),
	[CLMT] = LE('CLMT'), [COBJ] = LE('COBJ'), [COLL] = LE('COLL'),
	[CONT] = LE('CONT'), [CPTH] = LE('CPTH'), [CSTY] = LE('CSTY'),
	[DEBR] = LE('DEBR'), [DIAL] = LE('DIAL'), [DLBR] = LE('DLBR'),
	[DLVW] = LE('DLVW'), [DOBJ] = LE('DOBJ'), [DOOR] = LE('DOOR'),
	[DUAL] = LE('DUAL'), [ECZN] = LE('ECZN'), [EFSH] = LE('EFSH'),
	[ENCH] = LE('ENCH'), [EQUP] = LE('EQUP'), [EXPL] = LE('EXPL'),
	[EYES] = LE('EYES'), [FACT] = LE('FACT'), [FLOR] = LE('FLOR'),
	[FLST] = LE('FLST'), [FSTP] = LE('FSTP'), [FSTS] = LE('FSTS'),
	[FURN] = LE('FURN'), [GLOB] = LE('GLOB'), [GMST] = LE('GMST'),
	[GRAS] = LE('GRAS'), [GRUP] = LE('GRUP'), [HAIR] = LE('HAIR'),
	[HAZD] = LE('HAZD'), [HDPT] = LE('HDPT'), [IDLE] = LE('IDLE'),
	[IDLM] = LE('IDLM'), [IMAD] = LE('IMAD'), [IMGS] = LE('IMGS'),
	[INFO] = LE('INFO'), [INGR] = LE('INGR'), [IPCT] = LE('IPCT'),
	[IPDS] = LE('IPDS'), [KEYM] = LE('KEYM'), [KYWD] = LE('KYWD'),
	[LAND] = LE('LAND'), [LCRT] = LE('LCRT'), [LCTN] = LE('LCTN'),
	[LGTM] = LE('LGTM'), [LIGH] = LE('LIGH'), [LSCR] = LE('LSCR'),
	[LTEX] = LE('LTEX'), [LVLI] = LE('LVLI'), [LVLN] = LE('LVLN'),
	[LVSP] = LE('LVSP'), [MATO] = LE('MATO'), [MATT] = LE('MATT'),
	[MESG] = LE('MESG'), [MGEF] = LE('MGEF'), [MISC] = LE('MISC'),
	[MOVT] = LE('MOVT'), [MSTT] = LE('MSTT'), [MUSC] = LE('MUSC'),
	[MUST] = LE('MUST'), [NAVI] = LE('NAVI'), [NAVM] = LE('NAVM'),
	[NOTE] = LE('NOTE'), [NPC_] = LE('NPC_'), [OTFT] = LE('OTFT'),
	[PACK] = LE('PACK'), [PERK] = LE('PERK'), [PGRE] = LE('PGRE'),
	[PHZD] = LE('PHZD'), [PROJ] = LE('PROJ'), [PWAT] = LE('PWAT'),
	[QUST] = LE('QUST'), [RACE] = LE('RACE'), [REFR] = LE('REFR'),
	[REGN] = LE('REGN'), [RELA] = LE('RELA'), [REVB] = LE('REVB'),
	[RFCT] = LE('RFCT'), [RGDL] = LE('RGDL'), [SCEN] = LE('SCEN'),
	[SCOL] = LE('SCOL'), [SCPT] = LE('SCPT'), [SCRL] = LE('SCRL'),
	[SHOU] = LE('SHOU'), [SLGM] = LE('SLGM'), [SMBN] = LE('SMBN'),
	[SMEN] = LE('SMEN'), [SMQN] = LE('SMQN'), [SNCT] = LE('SNCT'),
	[SNDR] = LE('SNDR'), [SOPM] = LE('SOPM'), [SOUN] = LE('SOUN'),
	[SPEL] = LE('SPEL'), [SPGD] = LE('SPGD'), [STAT] = LE('STAT'),
	[TACT] = LE('TACT'), [TES4] = LE('TES4'), [TREE] = LE('TREE'),
	[TXST] = LE('TXST'), [VTYP] = LE('VTYP'), [WATR] = LE('WATR'),
	[WEAP] = LE('WEAP'), [WOOP] = LE('WOOP'), [WRLD] = LE('WRLD'),
	[WTHR] = LE('WTHR'),
};

const uint16_t rt2rth[RT_SIZE] = {
	[AACT] = RT_AACT,[ACHR] = RT_ACHR,[ACTI] = RT_ACTI,
	[ADDN] = RT_ADDN,[ALCH] = RT_ALCH,[AMMO] = RT_AMMO,
	[ANIO] = RT_ANIO,[APPA] = RT_APPA,[ARMA] = RT_ARMA,
	[ARMO] = RT_ARMO,[ARTO] = RT_ARTO,[ASPC] = RT_ASPC,
	[ASTP] = RT_ASTP,[AVIF] = RT_AVIF,[BOOK] = RT_BOOK,
	[BPTD] = RT_BPTD,[CAMS] = RT_CAMS,[CELL] = RT_CELL,
	[CLAS] = RT_CLAS,[CLDC] = RT_CLDC,[CLFM] = RT_CLFM,
	[CLMT] = RT_CLMT,[COBJ] = RT_COBJ,[COLL] = RT_COLL,
	[CONT] = RT_CONT,[CPTH] = RT_CPTH,[CSTY] = RT_CSTY,
	[DEBR] = RT_DEBR,[DIAL] = RT_DIAL,[DLBR] = RT_DLBR,
	[DLVW] = RT_DLVW,[DOBJ] = RT_DOBJ,[DOOR] = RT_DOOR,
	[DUAL] = RT_DUAL,[ECZN] = RT_ECZN,[EFSH] = RT_EFSH,
	[ENCH] = RT_ENCH,[EQUP] = RT_EQUP,[EXPL] = RT_EXPL,
	[EYES] = RT_EYES,[FACT] = RT_FACT,[FLOR] = RT_FLOR,
	[FLST] = RT_FLST,[FSTP] = RT_FSTP,[FSTS] = RT_FSTS,
	[FURN] = RT_FURN,[GLOB] = RT_GLOB,[GMST] = RT_GMST,
	[GRAS] = RT_GRAS,[GRUP] = RT_GRUP,[HAIR] = RT_HAIR,
	[HAZD] = RT_HAZD,[HDPT] = RT_HDPT,[IDLE] = RT_IDLE,
	[IDLM] = RT_IDLM,[IMAD] = RT_IMAD,[IMGS] = RT_IMGS,
	[INFO] = RT_INFO,[INGR] = RT_INGR,[IPCT] = RT_IPCT,
	[IPDS] = RT_IPDS,[KEYM] = RT_KEYM,[KYWD] = RT_KYWD,
	[LAND] = RT_LAND,[LCRT] = RT_LCRT,[LCTN] = RT_LCTN,
	[LGTM] = RT_LGTM,[LIGH] = RT_LIGH,[LSCR] = RT_LSCR,
	[LTEX] = RT_LTEX,[LVLI] = RT_LVLI,[LVLN] = RT_LVLN,
	[LVSP] = RT_LVSP,[MATO] = RT_MATO,[MATT] = RT_MATT,
	[MESG] = RT_MESG,[MGEF] = RT_MGEF,[MISC] = RT_MISC,
	[MOVT] = RT_MOVT,[MSTT] = RT_MSTT,[MUSC] = RT_MUSC,
	[MUST] = RT_MUST,[NAVI] = RT_NAVI,[NAVM] = RT_NAVM,
	[NOTE] = RT_NOTE,[NPC_] = RT_NPC_,[OTFT] = RT_OTFT,
	[PACK] = RT_PACK,[PERK] = RT_PERK,[PGRE] = RT_PGRE,
	[PHZD] = RT_PHZD,[PROJ] = RT_PROJ,[PWAT] = RT_PWAT,
	[QUST] = RT_QUST,[RACE] = RT_RACE,[REFR] = RT_REFR,
	[REGN] = RT_REGN,[RELA] = RT_RELA,[REVB] = RT_REVB,
	[RFCT] = RT_RFCT,[RGDL] = RT_RGDL,[SCEN] = RT_SCEN,
	[SCOL] = RT_SCOL,[SCPT] = RT_SCPT,[SCRL] = RT_SCRL,
	[SHOU] = RT_SHOU,[SLGM] = RT_SLGM,[SMBN] = RT_SMBN,
	[SMEN] = RT_SMEN,[SMQN] = RT_SMQN,[SNCT] = RT_SNCT,
	[SNDR] = RT_SNDR,[SOPM] = RT_SOPM,[SOUN] = RT_SOUN,
	[SPEL] = RT_SPEL,[SPGD] = RT_SPGD,[STAT] = RT_STAT,
	[TACT] = RT_TACT,[TES4] = RT_TES4,[TREE] = RT_TREE,
	[TXST] = RT_TXST,[VTYP] = RT_VTYP,[WATR] = RT_WATR,
	[WEAP] = RT_WEAP,[WOOP] = RT_WOOP,[WRLD] = RT_WRLD,
	[WTHR] = RT_WTHR,
};

const uint8_t rth2rt[RT_HASH_SIZE] = {
	[RT_AACT] = AACT,[RT_ACHR] = ACHR,[RT_ACTI] = ACTI,
	[RT_ADDN] = ADDN,[RT_ALCH] = ALCH,[RT_AMMO] = AMMO,
	[RT_ANIO] = ANIO,[RT_APPA] = APPA,[RT_ARMA] = ARMA,
	[RT_ARMO] = ARMO,[RT_ARTO] = ARTO,[RT_ASPC] = ASPC,
	[RT_ASTP] = ASTP,[RT_AVIF] = AVIF,[RT_BOOK] = BOOK,
	[RT_BPTD] = BPTD,[RT_CAMS] = CAMS,[RT_CELL] = CELL,
	[RT_CLAS] = CLAS,[RT_CLDC] = CLDC,[RT_CLFM] = CLFM,
	[RT_CLMT] = CLMT,[RT_COBJ] = COBJ,[RT_COLL] = COLL,
	[RT_CONT] = CONT,[RT_CPTH] = CPTH,[RT_CSTY] = CSTY,
	[RT_DEBR] = DEBR,[RT_DIAL] = DIAL,[RT_DLBR] = DLBR,
	[RT_DLVW] = DLVW,[RT_DOBJ] = DOBJ,[RT_DOOR] = DOOR,
	[RT_DUAL] = DUAL,[RT_ECZN] = ECZN,[RT_EFSH] = EFSH,
	[RT_ENCH] = ENCH,[RT_EQUP] = EQUP,[RT_EXPL] = EXPL,
	[RT_EYES] = EYES,[RT_FACT] = FACT,[RT_FLOR] = FLOR,
	[RT_FLST] = FLST,[RT_FSTP] = FSTP,[RT_FSTS] = FSTS,
	[RT_FURN] = FURN,[RT_GLOB] = GLOB,[RT_GMST] = GMST,
	[RT_GRAS] = GRAS,[RT_GRUP] = GRUP,[RT_HAIR] = HAIR,
	[RT_HAZD] = HAZD,[RT_HDPT] = HDPT,[RT_IDLE] = IDLE,
	[RT_IDLM] = IDLM,[RT_IMAD] = IMAD,[RT_IMGS] = IMGS,
	[RT_INFO] = INFO,[RT_INGR] = INGR,[RT_IPCT] = IPCT,
	[RT_IPDS] = IPDS,[RT_KEYM] = KEYM,[RT_KYWD] = KYWD,
	[RT_LAND] = LAND,[RT_LCRT] = LCRT,[RT_LCTN] = LCTN,
	[RT_LGTM] = LGTM,[RT_LIGH] = LIGH,[RT_LSCR] = LSCR,
	[RT_LTEX] = LTEX,[RT_LVLI] = LVLI,[RT_LVLN] = LVLN,
	[RT_LVSP] = LVSP,[RT_MATO] = MATO,[RT_MATT] = MATT,
	[RT_MESG] = MESG,[RT_MGEF] = MGEF,[RT_MISC] = MISC,
	[RT_MOVT] = MOVT,[RT_MSTT] = MSTT,[RT_MUSC] = MUSC,
	[RT_MUST] = MUST,[RT_NAVI] = NAVI,[RT_NAVM] = NAVM,
	[RT_NOTE] = NOTE,[RT_NPC_] = NPC_,[RT_OTFT] = OTFT,
	[RT_PACK] = PACK,[RT_PERK] = PERK,[RT_PGRE] = PGRE,
	[RT_PHZD] = PHZD,[RT_PROJ] = PROJ,[RT_PWAT] = PWAT,
	[RT_QUST] = QUST,[RT_RACE] = RACE,[RT_REFR] = REFR,
	[RT_REGN] = REGN,[RT_RELA] = RELA,[RT_REVB] = REVB,
	[RT_RFCT] = RFCT,[RT_RGDL] = RGDL,[RT_SCEN] = SCEN,
	[RT_SCOL] = SCOL,[RT_SCPT] = SCPT,[RT_SCRL] = SCRL,
	[RT_SHOU] = SHOU,[RT_SLGM] = SLGM,[RT_SMBN] = SMBN,
	[RT_SMEN] = SMEN,[RT_SMQN] = SMQN,[RT_SNCT] = SNCT,
	[RT_SNDR] = SNDR,[RT_SOPM] = SOPM,[RT_SOUN] = SOUN,
	[RT_SPEL] = SPEL,[RT_SPGD] = SPGD,[RT_STAT] = STAT,
	[RT_TACT] = TACT,[RT_TES4] = TES4,[RT_TREE] = TREE,
	[RT_TXST] = TXST,[RT_VTYP] = VTYP,[RT_WATR] = WATR,
	[RT_WEAP] = WEAP,[RT_WOOP] = WOOP,[RT_WRLD] = WRLD,
	[RT_WTHR] = WTHR,
};

const char *const group_type_strings[GTS_SIZE] = {
	"Top Type",
	"World Children",
	"Interior Cell Block",
	"Interior Cell Sub-Block",
	"Exterior Cell",
	"Exterior Cell Sub-Block",
	"Cell Children",
	"Topic Children",
	"Cell Persistent Children",
	"Cell Temporary Children",
};

// Non-REFR flags
rfs_inner _achr = {
	[9] = "Starts Dead",
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[25] = "No AI Acquire",
	[29] = "Don't Havok Settle",
	[30] = "ACHR Unknown 30",
};

rfs_inner _acti = {
	[6] = "Has Tree LOD",
	[8] = "Must Update Anims",
	[9] = "Hidden From Local Map",
	[15] = "Has Distant LOD",
	[16] = "Random Anim Start",
	[17] = "Dangerous",
	[20] = "Ignore Object Interaction",
	[23] = "Is Marker",
	[25] = "Obstacle",
	[26] = "NavMesh Generation - Filter",
	[27] = "NavMesh Generation - Bounding Box",
	[29] = "Child Can Use",
	[30] = "NavMesh Generation - Ground",
};

rfs_inner _tact = {
	[9] = "Hidden From Local Map",
	[16] = "Random Anim Start",
	[17] = "Radio Station",
};

rfs_inner _alch = {
	[29] = "Medicine",
};

rfs_inner _ammo = {
	[2] = "Non-Playable",
};

rfs_inner _anio = {
	[9] = "ANIO Unknown 9",
};

rfs_inner _armo = {
	[2] = "Non-Playable",
	[6] = "Shield",
	[10] = "ARMO Unknown 10",
	[15] = "ARMO Unknown 15",
};

#define REFERENCE_RECORD { \
	[7] = "Turn Off Fire", \
	[10] = "Persistent", \
	[11] = "Initially Disabled", \
	[28] = "Reflected by Auto Water", \
	[30] = "No Respawn", \
}

rfs_inner _parw = REFERENCE_RECORD;
rfs_inner _pbar = REFERENCE_RECORD;
rfs_inner _pbea = REFERENCE_RECORD;
rfs_inner _pcon = REFERENCE_RECORD;
rfs_inner _pfla = REFERENCE_RECORD;
rfs_inner _pgre = REFERENCE_RECORD;
rfs_inner _phzd = REFERENCE_RECORD;
rfs_inner _pmis = REFERENCE_RECORD;

#undef REFERENCE_RECORD

rfs_inner _cell = {
	[10] = "Persistent",
	[17] = "Off Limits",
	[18] = "Compressed",
	[19] = "Can't Wait",
};

rfs_inner _cont = {
	[15] = "Has Distant LOD",
	[16] = "Random Anim Start",
	[25] = "Obstacle",
	[26] = "NavMesh Generation - Filter",
	[27] = "NavMesh Generation - Bounding Box",
	[30] = "NavMesh Generation - Ground",
};

rfs_inner _csty = {
	[19] = "Allow Dual Wielding",
};

rfs_inner _door = {
	[15] = "Has Distant LOD",
	[16] = "Random Anim Start",
	[23] = "Is Marker",
};

rfs_inner _eyes = {
	[2] = "Non-Playable",
};

rfs_inner _furn = {
	[7] = "Is Perch",
	[16] = "Has Distant LOD",
	[23] = "Is Marker",
	[28] = "Must Exit To Talk",
	[29] = "Child Can Use",
};

rfs_inner _glob = {
	[6] = "Constant",
};

rfs_inner _hdpt = {
	[2] = "Non-Playable",
};

rfs_inner _mstt = {
	[8] = "Must Update Anims",
	[9] = "Hidden From Local Map",
	[15] = "Has Distant LOD",
	[16] = "Random Anim Start",
	[19] = "Has Currents",
	[25] = "Obstacle",
	[26] = "NavMesh Generation - Filter",
	[27] = "NavMesh Generation - Bounding Box",
	[30] = "NavMesh Generation - Ground",
};

rfs_inner _idlm = {
	[29] = "Child Can Use",
};

rfs_inner _slgm = {
	[17] = "Can Hold NPC Soul",
};

rfs_inner _navm = {
	[18] = "Compressed",
	[26] = "AutoGen",
	[31] = "NavmeshGenCell",
};

rfs_inner _perk = {
	[2] = "Non-Playable",
};

rfs_inner _shou = {
	[7] = "Treat Spells As Powers",
};

rfs_inner _rela = {
	[6] = "Secret",
};

rfs_inner _clfm = {
	[2] = "Non-Playable",
};

rfs_inner _info = {
	[13] = "Actor Changed",
};

rfs_inner _keym = {
	[2] = "Non-Playable",
};

rfs_inner _land = {
	[18] = "Compressed",
};

rfs_inner _ligh = {
	[16] = "Random Anim Start",
	[17] = "Portal-strict",
	[25] = "Obstacle",
};

rfs_inner _lscr = {
	[10] = "Displays In Main Menu",
};

rfs_inner _misc = {
	[2] = "Non-Playable",
};

rfs_inner _npc_ = {
	[10] = "NPC_ Unknown 10",
	[18] = "Compressed",
	[19] = "NPC_ Unknown 19",
	[29] = "Bleedout Override",
};

rfs_inner _race = {
	[19] = "Critter (?)",
};

// these are generic refr flags for any reference type not handled by the refr
// specific flag lut
rfs_inner _refr = {
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Is Full LOD",
	[26] = "Filter (Collision Geometry)",
	[27] = "Bounding Box (Collision Geometry)",
	[30] = "Ground",
	[31] = "Multibound",
};

rfs_inner _regn = {
	[6] = "Border Region",
};

rfs_inner _stat = {
	[2] = "Never Fades",
	[5] = "Deleted",
	[6] = "Has Tree LOD",
	[7] = "Add-On LOD Object",
	[9] = "Hidden From Local Map",
	[11] = "STAT Unknown 11",
	[15] = "Has Distant LOD",
	[16] = "STAT Unknown 16",
	[17] = "Uses HD LOD Texture",
	[19] = "Has Currents",
	[23] = "Is Marker",
	[25] = "Obstacle",
	[26] = "NavMesh Generation - Filter",
	[27] = "NavMesh Generation - Bounding Box",
	[28] = "Show In World Map",
	[30] = "NavMesh Generation - Ground",
};

rfs_inner _tes4 = {
	[0] = "ESM",
	[1] = "Altered",
	[2] = "Checked",
	[3] = "Active",
	[4] = "Optimized File",
	[5] = "Temp ID Owner",
	[7] = "Localized",
	[8] = "Precalc Data Only",
	[9] = "ESL",
};

rfs_inner _tree = {
	[15] = "Has Distant LOD",
};

rfs_inner _weap = {
	[2] = "Non-Playable",
};

rfs_inner _wrld = {
	[19] = "Can't Wait",
};

rfs_inner *const rfs[RT_HASH_SIZE] = {
	[RT_AACT] =  NULL, [RT_ACHR] = _achr, [RT_ACTI] = _acti,
	[RT_ADDN] =  NULL, [RT_ALCH] = _alch, [RT_AMMO] = _ammo,
	[RT_ANIO] = _anio, [RT_APPA] =  NULL, [RT_ARMA] =  NULL,
	[RT_ARMO] = _armo, [RT_ARTO] =  NULL, [RT_ASPC] =  NULL,
	[RT_ASTP] =  NULL, [RT_AVIF] =  NULL, [RT_BOOK] =  NULL,
	[RT_BPTD] =  NULL, [RT_CAMS] =  NULL, [RT_CELL] = _cell,
	[RT_CLAS] =  NULL, [RT_CLDC] =  NULL, [RT_CLFM] = _clfm,
	[RT_CLMT] =  NULL, [RT_COBJ] =  NULL, [RT_COLL] =  NULL,
	[RT_CONT] = _cont, [RT_CPTH] =  NULL, [RT_CSTY] = _csty,
	[RT_DEBR] =  NULL, [RT_DIAL] =  NULL, [RT_DLBR] =  NULL,
	[RT_DLVW] =  NULL, [RT_DOBJ] =  NULL, [RT_DOOR] = _door,
	[RT_DUAL] =  NULL, [RT_ECZN] =  NULL, [RT_EFSH] =  NULL,
	[RT_ENCH] =  NULL, [RT_EQUP] =  NULL, [RT_EXPL] =  NULL,
	[RT_EYES] = _eyes, [RT_FACT] =  NULL, [RT_FLOR] =  NULL,
	[RT_FLST] =  NULL, [RT_FSTP] =  NULL, [RT_FSTS] =  NULL,
	[RT_FURN] = _furn, [RT_GLOB] = _glob, [RT_GMST] =  NULL,
	[RT_GRAS] =  NULL, [RT_GRUP] =  NULL, [RT_HAIR] =  NULL,
	[RT_HAZD] =  NULL, [RT_HDPT] = _hdpt, [RT_IDLE] =  NULL,
	[RT_IDLM] = _idlm, [RT_IMAD] =  NULL, [RT_IMGS] =  NULL,
	[RT_INFO] = _info, [RT_INGR] =  NULL, [RT_IPCT] =  NULL,
	[RT_IPDS] =  NULL, [RT_KEYM] = _keym, [RT_KYWD] =  NULL,
	[RT_LAND] = _land, [RT_LCRT] =  NULL, [RT_LCTN] =  NULL,
	[RT_LGTM] =  NULL, [RT_LIGH] = _ligh, [RT_LSCR] = _lscr,
	[RT_LTEX] =  NULL, [RT_LVLI] =  NULL, [RT_LVLN] =  NULL,
	[RT_LVSP] =  NULL, [RT_MATO] =  NULL, [RT_MATT] =  NULL,
	[RT_MESG] =  NULL, [RT_MGEF] =  NULL, [RT_MISC] = _misc,
	[RT_MOVT] =  NULL, [RT_MSTT] = _mstt, [RT_MUSC] =  NULL,
	[RT_MUST] =  NULL, [RT_NAVI] =  NULL, [RT_NAVM] = _navm,
	[RT_NOTE] =  NULL, [RT_NPC_] = _npc_, [RT_OTFT] =  NULL,
	[RT_PACK] =  NULL, [RT_PERK] = _perk, [RT_PGRE] = _pgre,
	[RT_PHZD] = _phzd, [RT_PROJ] =  NULL, [RT_PWAT] =  NULL,
	[RT_QUST] =  NULL, [RT_RACE] = _race, [RT_REFR] = _refr,
	[RT_REGN] = _regn, [RT_RELA] = _rela, [RT_REVB] =  NULL,
	[RT_RFCT] =  NULL, [RT_RGDL] =  NULL, [RT_SCEN] =  NULL,
	[RT_SCOL] =  NULL, [RT_SCPT] =  NULL, [RT_SCRL] =  NULL,
	[RT_SHOU] = _shou, [RT_SLGM] = _slgm, [RT_SMBN] =  NULL,
	[RT_SMEN] =  NULL, [RT_SMQN] =  NULL, [RT_SNCT] =  NULL,
	[RT_SNDR] =  NULL, [RT_SOPM] =  NULL, [RT_SOUN] =  NULL,
	[RT_SPEL] =  NULL, [RT_SPGD] =  NULL, [RT_STAT] = _stat,
	[RT_TACT] = _tact, [RT_TES4] = _tes4, [RT_TREE] = _tree,
	[RT_TXST] =  NULL, [RT_VTYP] =  NULL, [RT_WATR] =  NULL,
	[RT_WEAP] = _weap, [RT_WOOP] =  NULL, [RT_WRLD] = _wrld,
	[RT_WTHR] =  NULL,
};

// REFR flags depend on what its NAME field references
#define REFR_GROUP1 { \
	[9] = "Hidden From Local Map", \
	[10] = "Persistent", \
	[11] = "Initially Disabled", \
	[13] = "Sky Marker", \
	[15] = "Visible When Distant", \
	[16] = "Is Full LOD", \
	[26] = "Filter (Collision Geometry)", \
	[27] = "Bounding Box (Collision Geometry)", \
	[28] = "Reflected By Auto Water", \
	[29] = "Don't Havok Settle", \
	[30] = "No Respawn", \
	[31] = "Multibound", \
}

rfs_inner _refr_acti = REFR_GROUP1;
rfs_inner _refr_stat = REFR_GROUP1;
rfs_inner _refr_tree = REFR_GROUP1; 
rfs_inner _refr_flor = REFR_GROUP1; 

#undef REFR_GROUP1

rfs_inner _refr_cont = {
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Is Full LOD",
	[25] = "No AI Acquire",
	[26] = "Filter (Collision Geometry)",
	[27] = "Bounding Box (Collision Geometry)",
	[28] = "Reflected By Auto Water",
	[29] = "Don't Havok Settle",
	[30] = "Ground",
	[31] = "Multibound",
};

rfs_inner _refr_door = {
	[6] = "Hidden From Local Map",
	[8] = "Inaccessible",
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Is Full LOD",
	[26] = "Filter (Collision Geometry)",
	[27] = "Bounding Box (Collision Geometry)",
	[28] = "Reflected By Auto Water",
	[29] = "Don't Havok Settle",
	[30] = "No Respawn",
	[31] = "Multibound",
};

rfs_inner _refr_ligh = {
	[8] = "Doesn't Light Water",
	[9] = "Casts Shadows",
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Never Fades",
	[17] = "Doesn't Light Landscape",
	[25] = "No AI Acquire",
	[28] = "Reflected By Auto Water",
	[29] = "Don't Havok Settle",
	[30] = "No Respawn",
	[31] = "Multibound",
};

rfs_inner _refr_mstt = {
	[9] = "Motion Blur",
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Is Full LOD",
	[26] = "Filter (Collision Geometry)",
	[27] = "Bounding Box (Collision Geometry)",
	[28] = "Reflected By Auto Water",
	[29] = "Don't Havok Settle",
	[30] = "No Respawn",
	[31] = "Multibound",
};

rfs_inner _refr_addn = {
	[10] = "Persistent",
	[11] = "Initially Disabled",
	[16] = "Is Full LOD",
	[28] = "Reflected By Auto Water",
	[29] = "Don't Havok Settle",
	[30] = "No Respawn",
	[31] = "Multibound",
};

#define REFR_GROUP2 { \
	[10] = "Persistent", \
	[11] = "Initially Disabled", \
	[16] = "Is Full LOD", \
	[25] = "No AI Acquire", \
	[28] = "Reflected By Auto Water", \
	[29] = "Don't Havok Settle", \
	[30] = "No Respawn", \
	[31] = "Multibound", \
}

rfs_inner _refr_alch = REFR_GROUP2;
rfs_inner _refr_scrl = REFR_GROUP2;
rfs_inner _refr_ammo = REFR_GROUP2;
rfs_inner _refr_armo = REFR_GROUP2;
rfs_inner _refr_ingr = REFR_GROUP2;
rfs_inner _refr_keym = REFR_GROUP2;
rfs_inner _refr_misc = REFR_GROUP2;
rfs_inner _refr_slgm = REFR_GROUP2;
rfs_inner _refr_weap = REFR_GROUP2;

rfs_inner *const rfs_refr[] = {
	[RT_AACT] = NULL      ,[RT_ACHR] = NULL      ,[RT_ACTI] = _refr_acti,
	[RT_ADDN] = _refr_addn,[RT_ALCH] = _refr_alch,[RT_AMMO] = _refr_ammo,
	[RT_ANIO] = NULL      ,[RT_APPA] = NULL      ,[RT_ARMA] = NULL      ,
	[RT_ARMO] = _refr_armo,[RT_ARTO] = NULL      ,[RT_ASPC] = NULL      ,
	[RT_ASTP] = NULL      ,[RT_AVIF] = NULL      ,[RT_BOOK] = NULL      ,
	[RT_BPTD] = NULL      ,[RT_CAMS] = NULL      ,[RT_CELL] = NULL      ,
	[RT_CLAS] = NULL      ,[RT_CLDC] = NULL      ,[RT_CLFM] = NULL      ,
	[RT_CLMT] = NULL      ,[RT_COBJ] = NULL      ,[RT_COLL] = NULL      ,
	[RT_CONT] = _refr_cont,[RT_CPTH] = NULL      ,[RT_CSTY] = NULL      ,
	[RT_DEBR] = NULL      ,[RT_DIAL] = NULL      ,[RT_DLBR] = NULL      ,
	[RT_DLVW] = NULL      ,[RT_DOBJ] = NULL      ,[RT_DOOR] = _refr_door,
	[RT_DUAL] = NULL      ,[RT_ECZN] = NULL      ,[RT_EFSH] = NULL      ,
	[RT_ENCH] = NULL      ,[RT_EQUP] = NULL      ,[RT_EXPL] = NULL      ,
	[RT_EYES] = NULL      ,[RT_FACT] = NULL      ,[RT_FLOR] = _refr_flor,
	[RT_FLST] = NULL      ,[RT_FSTP] = NULL      ,[RT_FSTS] = NULL      ,
	[RT_FURN] = NULL      ,[RT_GLOB] = NULL      ,[RT_GMST] = NULL      ,
	[RT_GRAS] = NULL      ,[RT_GRUP] = NULL      ,[RT_HAIR] = NULL      ,
	[RT_HAZD] = NULL      ,[RT_HDPT] = NULL      ,[RT_IDLE] = NULL      ,
	[RT_IDLM] = NULL      ,[RT_IMAD] = NULL      ,[RT_IMGS] = NULL      ,
	[RT_INFO] = NULL      ,[RT_INGR] = _refr_ingr,[RT_IPCT] = NULL      ,
	[RT_IPDS] = NULL      ,[RT_KEYM] = _refr_keym,[RT_KYWD] = NULL      ,
	[RT_LAND] = NULL      ,[RT_LCRT] = NULL      ,[RT_LCTN] = NULL      ,
	[RT_LGTM] = NULL      ,[RT_LIGH] = _refr_ligh,[RT_LSCR] = NULL      ,
	[RT_LTEX] = NULL      ,[RT_LVLI] = NULL      ,[RT_LVLN] = NULL      ,
	[RT_LVSP] = NULL      ,[RT_MATO] = NULL      ,[RT_MATT] = NULL      ,
	[RT_MESG] = NULL      ,[RT_MGEF] = NULL      ,[RT_MISC] = _refr_misc,
	[RT_MOVT] = NULL      ,[RT_MSTT] = _refr_mstt,[RT_MUSC] = NULL      ,
	[RT_MUST] = NULL      ,[RT_NAVI] = NULL      ,[RT_NAVM] = NULL      ,
	[RT_NOTE] = NULL      ,[RT_NPC_] = NULL      ,[RT_OTFT] = NULL      ,
	[RT_PACK] = NULL      ,[RT_PERK] = NULL      ,[RT_PGRE] = NULL      ,
	[RT_PHZD] = NULL      ,[RT_PROJ] = NULL      ,[RT_PWAT] = NULL      ,
	[RT_QUST] = NULL      ,[RT_RACE] = NULL      ,[RT_REFR] = NULL      ,
	[RT_REGN] = NULL      ,[RT_RELA] = NULL      ,[RT_REVB] = NULL      ,
	[RT_RFCT] = NULL      ,[RT_RGDL] = NULL      ,[RT_SCEN] = NULL      ,
	[RT_SCOL] = NULL      ,[RT_SCPT] = NULL      ,[RT_SCRL] = _refr_scrl,
	[RT_SHOU] = NULL      ,[RT_SLGM] = _refr_slgm,[RT_SMBN] = NULL      ,
	[RT_SMEN] = NULL      ,[RT_SMQN] = NULL      ,[RT_SNCT] = NULL      ,
	[RT_SNDR] = NULL      ,[RT_SOPM] = NULL      ,[RT_SOUN] = NULL      ,
	[RT_SPEL] = NULL      ,[RT_SPGD] = NULL      ,[RT_STAT] = _refr_stat,
	[RT_TACT] = NULL      ,[RT_TES4] = NULL      ,[RT_TREE] = _refr_tree,
	[RT_TXST] = NULL      ,[RT_VTYP] = NULL      ,[RT_WATR] = NULL      ,
	[RT_WEAP] = _refr_weap,[RT_WOOP] = NULL      ,[RT_WRLD] = NULL      ,
	[RT_WTHR] = NULL      ,
};

