forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp_goldsrc.hexpat
210 lines (181 loc) · 5.02 KB
/
bsp_goldsrc.hexpat
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
#pragma description GoldSrc engine maps format (used in Half-Life 1)
import std.ptr;
import std.mem;
import std.sys;
#pragma endian little
#define HEADER_LUMPS 15
#define MAX_MAP_HULLS 4
#define NUM_AMBIENTS 4
#define MAXLIGHTMAPS 4
enum LumpIndex : u32 {
Entities,
Planes,
Textures,
Vertexes,
Visibility,
Nodes,
Texinfo,
Faces,
Lighting,
Clipnodes,
Leafs,
Marksurfaces,
Edges,
Surfedges,
Models,
};
struct vec3f
{
float x, y, z;
};
struct dlump_t
{
s32 fileofs;
s32 filelen;
};
struct dheader_t
{
s32 version;
dlump_t lumps[HEADER_LUMPS];
std::assert(version == 30, "This version of BSP format is unsupported.");
};
struct dmodel_t
{
vec3f mins;
vec3f maxs;
vec3f origin; // for sounds or lights
s32 headnode[MAX_MAP_HULLS];
s32 visleafs; // not including the solid leaf 0
s32 firstface;
s32 numfaces;
};
struct dplane_t
{
vec3f normal;
float dist;
s32 type;
};
struct dtexinfo_t
{
float vecs[8]; // texmatrix [s/t][xyz offset]
s32 miptex;
s16 flags;
s16 faceinfo; // -1 no face info otherwise dfaceinfo_t
};
struct dleaf_t
{
s32 contents;
s32 visofs; // -1 = no visibility info
s16 mins[3];
s16 maxs[3];
u16 firstmarksurface;
u16 nummarksurfaces;
u8 ambient_level[NUM_AMBIENTS];
};
struct dnode_t
{
s32 planenum;
s16 children[2]; // negative numbers are -(leafs + 1), not nodes
s16 mins[3];
s16 maxs[3];
u16 firstface;
u16 numfaces; // counting both sides
};
struct dface_t
{
u16 planenum;
s16 side;
s32 firstedge;
s16 numedges;
s16 texinfo;
u8 styles[MAXLIGHTMAPS];
s32 lightofs; // start of [numstyles*surfsize] samples
};
struct dedge_t
{
u16 v[2]; // indices of vertexes
};
struct dclipnode_t
{
s32 planenum;
s16 children[2];
};
using dmarkface_t = u16;
using dsurfedge_t = s32;
using dvertex_t = vec3f;
fn get_miptex_pixeldata_size(auto width, auto height, auto offset) {
if (offset != 0) {
return std::mem::align_to(4, width * height * 85 / 64 + sizeof(u16) + 768);
}
else {
return 0;
}
};
struct miptex_t
{
char name[16];
u32 width;
u32 height;
u32 offsets[4]; // four mip maps stored
u8 pixeldata[get_miptex_pixeldata_size(width, height, offsets[0])];
};
dheader_t file_header @ 0x00;
fn get_lump_element_count(auto index, auto elem_size) {
return file_header.lumps[index].filelen / elem_size;
};
fn get_lump_address(auto index) {
return file_header.lumps[index].fileofs;
};
fn get_miptex_ptr_base(auto offset) {
return file_header.lumps[LumpIndex::Textures].fileofs;
};
struct MiptexPointer
{
miptex_t *data: u32 [[pointer_base("get_miptex_ptr_base"), inline]];
};
struct dmiptexlump_t
{
s32 nummiptex;
MiptexPointer dataofs[nummiptex];
};
struct VisibilityData
{
u8 data[file_header.lumps[LumpIndex::Visibility].filelen];
u8 pad[std::mem::align_to(4, sizeof(this)) - sizeof(this)];
};
struct LightmapData
{
u8 data[file_header.lumps[LumpIndex::Lighting].filelen];
u8 pad[std::mem::align_to(4, sizeof(this)) - sizeof(this)];
};
struct EntityData
{
char text[];
u8 pad[std::mem::align_to(4, sizeof(this)) - sizeof(this)];
};
s32 models_count = get_lump_element_count(LumpIndex::Models, sizeof(dmodel_t));
s32 vertexes_count = get_lump_element_count(LumpIndex::Vertexes, sizeof(vec3f));
s32 planes_count = get_lump_element_count(LumpIndex::Planes, sizeof(dplane_t));
s32 leafs_count = get_lump_element_count(LumpIndex::Leafs, sizeof(dleaf_t));
s32 nodes_count = get_lump_element_count(LumpIndex::Nodes, sizeof(dnode_t));
s32 faces_count = get_lump_element_count(LumpIndex::Faces, sizeof(dface_t));
s32 markfaces_count = get_lump_element_count(LumpIndex::Marksurfaces, sizeof(dmarkface_t));
s32 surfedges_count = get_lump_element_count(LumpIndex::Surfedges, sizeof(dsurfedge_t));
s32 edges_count = get_lump_element_count(LumpIndex::Edges, sizeof(dedge_t));
s32 clipnodes_count = get_lump_element_count(LumpIndex::Clipnodes, sizeof(dclipnode_t));
s32 texinfo_count = get_lump_element_count(LumpIndex::Texinfo, sizeof(dtexinfo_t));
dmodel_t models_lump[models_count] @ get_lump_address(LumpIndex::Models);
dvertex_t vertexes_lump[vertexes_count] @ get_lump_address(LumpIndex::Vertexes);
dplane_t planes_lump[planes_count] @ get_lump_address(LumpIndex::Planes);
dleaf_t leafs_lump[leafs_count] @ get_lump_address(LumpIndex::Leafs);
dnode_t nodes_lump[nodes_count] @ get_lump_address(LumpIndex::Nodes);
dface_t faces_lump[faces_count] @ get_lump_address(LumpIndex::Faces);
dmarkface_t markfaces_lump[markfaces_count] @ get_lump_address(LumpIndex::Marksurfaces);
dsurfedge_t surfedges_lump[surfedges_count] @ get_lump_address(LumpIndex::Surfedges);
dedge_t edges_lump[edges_count] @ get_lump_address(LumpIndex::Edges);
dclipnode_t clipnodes_lump[clipnodes_count] @ get_lump_address(LumpIndex::Clipnodes);
dtexinfo_t texinfo_lump[texinfo_count] @ get_lump_address(LumpIndex::Texinfo);
dmiptexlump_t textures_lump @ get_lump_address(LumpIndex::Textures);
VisibilityData visdata_lump @ get_lump_address(LumpIndex::Visibility);
LightmapData lightdata_lump @ get_lump_address(LumpIndex::Lighting);
EntityData entdata_lump @ get_lump_address(LumpIndex::Entities);