-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockmap.c
415 lines (310 loc) · 9.66 KB
/
blockmap.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*- blockmap.c --------------------------------------------------------------*
Node builder for DOOM levels (c) 1998 Colin Reed, version 3.0 (dos extended)
Credit to:-
Raphael Quinet (A very small amount of code has been borrowed from DEU).
Matt Fell for the doom specs.
Lee Killough for performance tuning, support for multilevel wads, special
effects, and wads with lumps besides levels.
Also, the original idea for some of the techniques where also taken from the
comment at the bottom of OBJECTS.C in DEU, and the doc by Matt Fell about
the nodes.
Use this code for your own editors, but please credit me.
*---------------------------------------------------------------------------*/
/*
* Separated from bsp.c 2000/08/26 by Colin Phipps
*/
#include "structs.h"
#include "bsp.h"
/*--------------------------------------------------------------------------*/
static int
IsLineDefInside(int ldnum, int xmin, int ymin, int xmax, int ymax)
{
int x1 = vertices[linedefs[ldnum].start].x;
int y1 = vertices[linedefs[ldnum].start].y;
int x2 = vertices[linedefs[ldnum].end].x;
int y2 = vertices[linedefs[ldnum].end].y;
int count = 2;
for (;;)
if (y1 > ymax) {
if (y2 > ymax)
return (FALSE);
x1 = x1 + (x2 - x1) * (double) (ymax - y1) / (y2 - y1);
y1 = ymax;
count = 2;
} else if (y1 < ymin) {
if (y2 < ymin)
return (FALSE);
x1 = x1 + (x2 - x1) * (double) (ymin - y1) / (y2 - y1);
y1 = ymin;
count = 2;
} else if (x1 > xmax) {
if (x2 > xmax)
return (FALSE);
y1 = y1 + (y2 - y1) * (double) (xmax - x1) / (x2 - x1);
x1 = xmax;
count = 2;
} else if (x1 < xmin) {
if (x2 < xmin)
return (FALSE);
y1 = y1 + (y2 - y1) * (double) (xmin - x1) / (x2 - x1);
x1 = xmin;
count = 2;
} else {
int t;
if (!--count)
return (TRUE);
t = x1;
x1 = x2;
x2 = t;
t = y1;
y1 = y2;
y2 = t;
}
}
/*- Create blockmap --------------------------------------------------------*/
void
CreateBlockmap_old(const bbox_t bbox)
{
struct Block blockhead;
short int *blockptrs;
short int *blocklists = NULL;
long blockptrs_size;
long blockoffs = 0;
int x, y, n;
int blocknum = 0;
Verbose("Creating Blockmap... ");
blockhead.minx = bbox[BB_LEFT] & -8;
blockhead.miny = bbox[BB_BOTTOM] & -8;
blockhead.xblocks = ((bbox[BB_RIGHT] - (bbox[BB_LEFT] & -8)) / 128) + 1;
blockhead.yblocks = ((bbox[BB_TOP] - (bbox[BB_BOTTOM] & -8)) / 128) + 1;
blockptrs_size = (blockhead.xblocks * blockhead.yblocks) * 2;
blockptrs = GetMemory(blockptrs_size);
for (y = 0; y < blockhead.yblocks; y++) {
for (x = 0; x < blockhead.xblocks; x++) {
progress();
blockptrs[blocknum] = (blockoffs + 4 + (blockptrs_size / 2));
swapshort((unsigned short *) blockptrs + blocknum);
blocklists = ResizeMemory(blocklists, ((blockoffs + 1) * 2));
blocklists[blockoffs] = 0;
blockoffs++;
for (n = 0; n < num_lines; n++) {
if (IsLineDefInside(n, (blockhead.minx + (x * 128)), (blockhead.miny + (y * 128)), (blockhead.minx + (x * 128)) + 127, (blockhead.miny + (y * 128)) + 127)) {
/*
* printf("found line %d in block
* %d\n",n,blocknum);
*/
blocklists = ResizeMemory(blocklists, ((blockoffs + 1) * 2));
blocklists[blockoffs] = n;
swapshort((unsigned short *) blocklists + blockoffs);
blockoffs++;
}
}
blocklists = ResizeMemory(blocklists, ((blockoffs + 1) * 2));
blocklists[blockoffs] = -1;
swapshort((unsigned short *) blocklists + blockoffs);
blockoffs++;
blocknum++;
}
}
{
long blockmap_size = blockoffs * 2;
char *data = GetMemory(blockmap_size + blockptrs_size + 8);
memcpy(data, &blockhead, 8);
swapshort((unsigned short *) data + 0);
swapshort((unsigned short *) data + 1);
swapshort((unsigned short *) data + 2);
swapshort((unsigned short *) data + 3);
memcpy(data + 8, blockptrs, blockptrs_size);
memcpy(data + 8 + blockptrs_size, blocklists, blockmap_size);
free(blockptrs);
free(blocklists);
add_lump("BLOCKMAP", data, blockmap_size + blockptrs_size + 8);
}
Verbose("done.\n");
}
/*- Create blockmap (compressed) ----------------------------------------
* Contributed by Simon "fraggle" Howard <[email protected]>
* Merged 2001/11/17 */
struct blocklist_s {
int num_lines;
unsigned short offset;
unsigned short *lines;
struct blocklist_s *next; /* for hash table */
};
static struct blocklist_s **blockhash;
static int blockhash_size;
static int blocklist_size; /* size, in bytes of the blocklists */
/* offset pointers */
static struct blocklist_s **blockptrs;
static int num_blockptrs;
/* hashkey function for search */
static int blockhash_key(struct blocklist_s *bl)
{
int i;
int hash = 0;
/* this is a pretty lame hash function
it has to be associative though (ie, 1 2 3 == 3 2 1) */
for(i=0; i<bl->num_lines; ++i)
hash += bl->lines[i];
return hash % blockhash_size;
}
/* compare two struct blocklist_s's
like strcmp: a 0 return value means they are identical */
static int blocklist_cmp(struct blocklist_s *bl1, struct blocklist_s *bl2)
{
int i;
if(bl1->num_lines != bl2->num_lines)
return 1;
for(i=0; i<bl1->num_lines; ++i) {
if(bl1->lines[i] != bl2->lines[i])
return 1;
}
return 0;
}
/* search for an identical blocklist */
static struct blocklist_s *blockhash_search(struct blocklist_s *bl)
{
int hash = blockhash_key(bl);
struct blocklist_s *search;
for(search=blockhash[hash]; search; search=search->next) {
if(!blocklist_cmp(bl, search))
return search;
}
/* not found */
return NULL;
}
/* add a new blocklist to the hashtable */
static struct blocklist_s *blockhash_add(struct blocklist_s *newbl)
{
struct blocklist_s *bl;
int hash;
/* first, check an identical one doesnt already exist */
bl = blockhash_search(newbl);
if(bl)
return bl;
/* need to add new blocklist */
/* make a copy */
bl = GetMemory(sizeof(*bl));
bl->num_lines = newbl->num_lines;
bl->lines = GetMemory(sizeof(*bl->lines) * bl->num_lines);
memcpy(bl->lines, newbl->lines,
sizeof(*bl->lines) * bl->num_lines);
/* link into hash table */
hash = blockhash_key(bl);
bl->next = blockhash[hash];
blockhash[hash] = bl;
/* maintain blocklist count */
blocklist_size += sizeof(*bl->lines) * bl->num_lines;
return bl;
}
static void blockmap_assemble(const struct Block* blockmaphead)
{
int i;
int offset;
/* build the lump itself */
size_t blockmap_size =
sizeof(*blockmaphead) +
num_blockptrs * sizeof(short) +
blocklist_size;
register short* blockmap = GetMemory(blockmap_size);
/* header */
memcpy(blockmap, blockmaphead, sizeof *blockmaphead);
swapshort(blockmap);
swapshort(blockmap+1);
swapshort(blockmap+2);
swapshort(blockmap+3);
/* every hash chain */
for(i=0,offset=num_blockptrs+sizeof(*blockmaphead)/sizeof(short);
i<blockhash_size; ++i) {
struct blocklist_s *bl;
/* every blocklist in the chain */
for(bl=blockhash[i]; bl; bl = bl->next) {
/* write */
int l;
/* offset is in short ints, not bytes */
bl->offset = offset;
/* write each line */
for(l=0; l<bl->num_lines; ++l) {
blockmap[offset] = bl->lines[l];
swapshort(blockmap + offset);
offset++;
}
}
}
offset = sizeof(*blockmaphead)/sizeof(short);
for(i=0; i<num_blockptrs; ++i) {
blockmap[offset+i] = blockptrs[i]->offset;
swapshort(blockmap+offset+i);
}
add_lump("BLOCKMAP", blockmap, blockmap_size);
}
void
CreateBlockmap_compressed(const bbox_t bbox)
{
int x, y, n;
int blocknum = 0;
unsigned short *templines;
int num_templines;
struct Block blockhead;
fprintf(stderr,"Creating compressed blockmap... ");
/* header */
blockhead.minx = bbox[BB_LEFT] & -8;
blockhead.miny = bbox[BB_BOTTOM] & -8;
blockhead.xblocks = ((bbox[BB_RIGHT] - (bbox[BB_LEFT] & -8)) / 128) + 1;
blockhead.yblocks = ((bbox[BB_TOP] - (bbox[BB_BOTTOM] & -8)) / 128) + 1;
/* build hash table */
blockhash_size = blockhead.xblocks * blockhead.yblocks;
blockhash = GetMemory(blockhash_size * sizeof(*blockhash));
for(n=0; n<blockhash_size; ++n)
blockhash[n] = NULL;
/* pointers */
num_blockptrs = blockhead.xblocks * blockhead.yblocks;
blockptrs = GetMemory(num_blockptrs * sizeof(*blockptrs));
num_templines = 10240;
templines = GetMemory(num_templines * sizeof(*templines));
/* build all blocklists */
for (y = 0; y < blockhead.yblocks; y++) {
for (x = 0; x < blockhead.xblocks; x++) {
struct blocklist_s tempbl;
tempbl.num_lines = 0;
tempbl.lines = templines;
progress();
/* first line is a 0 */
tempbl.lines[tempbl.num_lines++] = 0;
for (n = 0; n < num_lines; n++) {
if (IsLineDefInside(n, (blockhead.minx + (x * 128)), (blockhead.miny + (y * 128)), (blockhead.minx + (x * 128)) + 127, (blockhead.miny + (y * 128)) + 127)) {
/*
* printf("found line %d in block
* %d\n",n,blocknum);
*/
if(tempbl.num_lines >= num_templines-5) {
num_lines *= 2;
templines = ResizeMemory(templines,
num_templines * sizeof(*templines));
tempbl.lines = templines;
}
tempbl.lines[tempbl.num_lines++] = n;
}
}
/* last is 0xffff */
tempbl.lines[tempbl.num_lines++] = 0xffff;
blockptrs[blocknum++] = blockhash_add(&tempbl);
}
}
free(templines);
/* assemble */
blockmap_assemble(&blockhead);
/* deconstruct the hash table */
for(n=0; n<blockhash_size; ++n) {
while(blockhash[n]) {
struct blocklist_s *del = blockhash[n];
blockhash[n] = blockhash[n]->next;
free(del->lines);
free(del);
}
}
free(blockhash);
free(blockptrs);
Verbose("done.\n");
return;
}