/
edit_map.cpp
1364 lines (1104 loc) · 36.5 KB
/
edit_map.cpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file edit_map.cpp Map Editor interface.
*
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2000-2007 Andrew Apted <ajapted@gmail.com>
* @authors Copyright © 1998-2000 Colin Reed <cph@moria.org.uk>
* @authors Copyright © 1998-2000 Lee Killough <killough@rsn.hp.com>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#define DENG_NO_API_MACROS_MAP_EDIT
#include <cmath>
#include "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "de_misc.h"
#include "de_edit.h"
#include "de_dam.h"
#include "de_filesys.h"
#include "map/gamemap.h"
#ifdef __CLIENT__
# include "render/rend_main.h"
#endif
#include <de/StringPool>
#include "audio/s_environ.h"
using namespace de;
#if 0
static int vertexCompare(void const *p1, void const *p2)
{
Vertex const *a = (Vertex const *) *((void const **) p1);
Vertex const *b = (Vertex const *) *((void const **) p2);
if(a == b) return 0;
if(int(a->origin()[VX]) != int(b->origin()[VX]))
return int(a->origin()[VX]) - int(b->origin()[VX]);
return int(a->origin()[VY]) - int(b->origin()[VY]);
}
#endif
/**
* @defgroup pruneMapElementFlags Prune Map Element Flags
* Flags for pruneMapElements()
*/
///@{
#define PRUNE_LINES 0x1
#define PRUNE_VERTEXES 0x2
#define PRUNE_SIDEDEFS 0x4
#define PRUNE_SECTORS 0x8
#define PRUNE_ALL (PRUNE_LINES|PRUNE_VERTEXES|PRUNE_SIDEDEFS|PRUNE_SECTORS)
///@}
/**
* Editable map data elements.
*
* @todo Move into GameMap.
*/
class EditableMap
{
public:
GameMap::Vertexes vertexes;
GameMap::Lines lines;
GameMap::SideDefs sideDefs;
GameMap::Sectors sectors;
GameMap::Polyobjs polyobjs;
/// Map entity data (things, line specials, etc...).
EntityDatabase *entityDatabase;
EditableMap::EditableMap()
: entityDatabase(0)
{}
Vertex *createVertex(coord_t x, coord_t y)
{
Vertex *vtx = new Vertex(x, y);
vertexes.append(vtx);
vtx->_buildData.index = vertexes.count(); // 1-based index, 0 = NIL.
return vtx;
}
LineDef *createLine()
{
LineDef *line = new LineDef;
lines.append(line);
line->_origIndex = lines.count(); // 1-based index, 0 = NIL.
return line;
}
SideDef *createSideDef()
{
SideDef *sideDef = new SideDef;
sideDefs.append(sideDef);
sideDef->_buildData.index = sideDefs.count(); // 1-based index, 0 = NIL.
return sideDef;
}
Sector *createSector(float lightLevel, Vector3f const &lightColor)
{
Sector *sector = new Sector(lightLevel, lightColor);
sectors.append(sector);
sector->_origIndex = sectors.count(); // 1-based index, 0 = NIL.
return sector;
}
Polyobj *createPolyobj()
{
void *region = M_Calloc(POLYOBJ_SIZE);
Polyobj *po = new (region) Polyobj;
polyobjs.append(po);
po->buildData.index = polyobjs.count(); // 1-based index, 0 = NIL.
return po;
}
void clear()
{
qDeleteAll(vertexes);
vertexes.clear();
qDeleteAll(lines);
lines.clear();
qDeleteAll(sideDefs);
sideDefs.clear();
qDeleteAll(sectors);
sectors.clear();
foreach(Polyobj *po, polyobjs)
{
po->~Polyobj();
M_Free(po);
}
polyobjs.clear();
}
#if 0
void markDuplicateVertexes()
{
Vertex **hits = (Vertex **) M_Malloc(vertexes.count() * sizeof(Vertex *));
// Sort array of ptrs.
for(uint i = 0; i < vertexes.count(); ++i)
{
hits[i] = vertexes[i];
}
qsort(hits, vertexes.count(), sizeof(Vertex *), vertexCompare);
// Now mark them off.
for(uint i = 0; i < vertexes.count() - 1; ++i)
{
// A duplicate?
if(vertexCompare(hits + i, hits + i + 1) == 0)
{
// Yes.
Vertex *a = hits[i];
Vertex *b = hits[i + 1];
b->_buildData.equiv = (a->_buildData.equiv ? a->_buildData.equiv : a);
}
}
M_Free(hits);
}
void findEquivalentVertexes()
{
uint i, newNum;
// Scan all linedefs.
for(i = 0, newNum = 0; i < lines.count(); ++i)
{
LineDef *l = lines[i];
// Handle duplicated vertices.
while(l->v[0]->_buildData.equiv)
{
l->v[0]->_buildData.refCount--;
l->v[0] = l->v[0]->_buildData.equiv;
l->v[0]->_buildData.refCount++;
}
while(l->v[1]->_buildData.equiv)
{
l->v[1]->_buildData.refCount--;
l->v[1] = l->v[1]->_buildData.equiv;
l->v[1]->_buildData.refCount++;
}
l->_buildData.index = newNum + 1;
lines[newNum++] = lines[i];
}
}
void pruneLinedefs()
{
uint i, newNum, unused = 0;
for(i = 0, newNum = 0; i < lines.count(); ++i)
{
LineDef *l = lines[i];
if(!l->hasFrontSideDef() && !l->hasBackSideDef())
{
unused++;
M_Free(l);
continue;
}
l->_buildData.index = newNum + 1;
lines[newNum++] = l;
}
if(newNum < lines.count())
{
if(unused > 0)
Con_Message(" Pruned %d unused linedefs.", unused);
lines.resize(newNum);
}
}
void pruneVertices()
{
uint i, newNum, unused = 0;
// Scan all vertices.
for(i = 0, newNum = 0; i < vertexes.count(); ++i)
{
Vertex *v = vertexes[i];
if(v->_buildData.refCount < 0)
Con_Error("Vertex %d ref_count is %d", i, v->_buildData.refCount);
if(v->_buildData.refCount == 0)
{
if(v->_buildData.equiv == NULL)
unused++;
M_Free(v);
continue;
}
v->_buildData.index = newNum + 1;
vertexes[newNum++] = v;
}
if(newNum < vertexes.count())
{
int dupNum = vertexes.count() - newNum - unused;
if(unused > 0)
Con_Message(" Pruned %d unused vertices.", unused);
if(dupNum > 0)
Con_Message(" Pruned %d duplicate vertices.", dupNum);
vertexes.resize(newNum);
}
}
void pruneUnusedSidedefs()
{
uint i, newNum, unused = 0;
for(i = 0, newNum = 0; i < sideDefs.count(); ++i)
{
SideDef *s = sideDefs[i];
if(s->buildData.refCount == 0)
{
unused++;
M_Free(s);
continue;
}
s->_buildData.index = newNum + 1;
sideDefs[newNum++] = s;
}
if(newNum < sideDefs.count())
{
int dupNum = sideDefs.count() - newNum - unused;
if(unused > 0)
Con_Message(" Pruned %d unused sidedefs.", unused);
if(dupNum > 0)
Con_Message(" Pruned %d duplicate sidedefs.", dupNum);
sideDefs.resize(newNum);
}
}
void pruneUnusedSectors()
{
for(uint i = 0; i < sideDefs.count(); ++i)
{
SideDef *s = sideDefs[i];
if(s->sector)
s->sector->buildData.refCount++;
}
// Scan all sectors.
uint newNum = 0;
for(uint i = 0; i < sectors.count(); ++i)
{
Sector* s = sectors[i];
if(s->buildData.refCount == 0)
{
M_Free(s);
continue;
}
s->buildData.index = newNum + 1;
sectors[newNum++] = s;
}
if(newNum < sectors.count())
{
Con_Message(" Pruned %d unused sectors.", sectors.count() - newNum);
sectors.resize(newNum);
}
}
/**
* @param flags @ref pruneMapElementFlags
*/
void pruneMapElements(int /*flags*/)
{
/**
* @todo Pruning cannot be done as game map data object properties
* are currently indexed by their original indices as determined by the
* position in the map data. The same problem occurs within ACS scripts
* and XG line/sector references.
*
* @warning Order here is critical!
*/
findEquivalentVertexes();
if(flags & PRUNE_LINES)
pruneLines();
if(flags & PRUNE_VERTEXES)
pruneVertices();
if(flags & PRUNE_SIDEDEFS)
pruneUnusedSidedefs();
if(flags & PRUNE_SECTORS)
pruneUnusedSectors();
}
#endif
};
static EditableMap editMap; // singleton
static bool editMapInited;
static bool lastBuiltMapResult;
static GameMap *lastBuiltMap;
int bspFactor = 7;
void MPE_Register()
{
C_VAR_INT("bsp-factor", &bspFactor, CVF_NO_MAX, 0, 0);
}
/**
* Material name references specified during map conversion are recorded in
* this dictionary. A dictionary is used to avoid repeatedly resolving the same
* URIs and to facilitate a log of missing materials encountered during the
* process.
*
* The pointer user value holds a pointer to the resolved Material (if found).
* The integer user value tracks the number of times a reference occurs.
*/
static StringPool *materialDict;
/**
* Either print or count-the-number-of unresolved references in the
* material dictionary.
*
* @param internId Unique id associated with the reference.
* @param parameters If a uint pointer operate in "count" mode (total written here).
* Else operate in "print" mode.
* @return Always @c 0 (for use as an iterator).
*/
static int printMissingMaterialWorker(StringPool::Id internId, void *parameters)
{
uint *count = (uint *)parameters;
// A valid id?
if(materialDict->string(internId))
{
// Have we resolved this reference yet?
if(!materialDict->userPointer(internId))
{
// An unresolved reference.
if(count)
{
// Count mode.
*count += 1;
}
else
{
// Print mode.
int const refCount = materialDict->userValue(internId);
String const &materialUri = materialDict->string(internId);
QByteArray materialUriUtf8 = materialUri.toUtf8();
Con_Message(" %4u x \"%s\"", refCount, materialUriUtf8.constData());
}
}
}
return 0; // Continue iteration.
}
/**
* Destroy the missing material dictionary.
*/
static void clearMaterialDict()
{
// Initialized?
if(!materialDict) return;
materialDict->clear();
delete materialDict; materialDict = 0;
}
/**
* Print any "missing" materials in the dictionary to the log.
*/
static void printMissingMaterialsInDict()
{
// Initialized?
if(!materialDict) return;
// Count missing materials.
uint numMissing = 0;
materialDict->iterate(printMissingMaterialWorker, &numMissing);
if(!numMissing) return;
Con_Message(" [110] Warning: Found %u unknown %s:", numMissing, numMissing == 1? "material":"materials");
// List the missing materials.
materialDict->iterate(printMissingMaterialWorker, 0);
}
/**
* Attempt to locate a material by its URI. A dictionary of previously
* searched-for URIs is maintained to avoid repeated searching and to record
* "missing" materials.
*
* @param materialUriStr URI of the material to search for.
*
* @return Pointer to the found material; otherwise @c 0.
*/
static Material *findMaterialInDict(ddstring_t const *materialUriStr)
{
if(!materialUriStr || Str_IsEmpty(materialUriStr)) return 0;
// Are we yet to instantiate the dictionary?
if(!materialDict)
{
materialDict = new StringPool;
}
de::Uri materialUri(Str_Text(materialUriStr), RC_NULL);
// Intern this reference.
StringPool::Id internId = materialDict->intern(materialUri.compose());
Material *material = 0;
// Have we previously encountered this?.
uint refCount = materialDict->userValue(internId);
if(refCount)
{
// Yes, if resolved the user pointer holds the found material.
material = (Material *) materialDict->userPointer(internId);
}
else
{
// No, attempt to resolve this URI and update the dictionary.
// First try the preferred scheme, then any.
try
{
material = &App_Materials().find(materialUri).material();
}
catch(Materials::NotFoundError const &)
{
// Try any scheme.
try
{
materialUri.setScheme("");
material = &App_Materials().find(materialUri).material();
}
catch(Materials::NotFoundError const &)
{}
}
// Insert the possibly resolved material into the dictionary.
materialDict->setUserPointer(internId, material);
}
// There is now one more reference.
refCount++;
materialDict->setUserValue(internId, refCount);
return material;
}
/// Used when sorting vertex line owners.
static Vertex *rootVtx;
/**
* Compares the angles of two lines that share a common vertex.
*
* pre: rootVtx must point to the vertex common between a and b
* which are (lineowner_t*) ptrs.
*/
static int lineAngleSorter(void const *a, void const *b)
{
binangle_t angles[2];
LineOwner *own[2] = { (LineOwner *)a, (LineOwner *)b };
for(uint i = 0; i < 2; ++i)
{
if(own[i]->_link[LineOwner::Previous]) // We have a cached result.
{
angles[i] = own[i]->angle();
}
else
{
LineDef *line = &own[i]->line();
Vertex const &otherVtx = line->vertex(&line->v1() == rootVtx? 1:0);
fixed_t dx = otherVtx.origin()[VX] - rootVtx->origin()[VX];
fixed_t dy = otherVtx.origin()[VY] - rootVtx->origin()[VY];
own[i]->_angle = angles[i] = bamsAtan2(-100 *dx, 100 * dy);
// Mark as having a cached angle.
own[i]->_link[LineOwner::Previous] = (LineOwner *) 1;
}
}
return (angles[1] - angles[0]);
}
/**
* Merge left and right line owner lists into a new list.
*
* @return The newly merged list.
*/
static LineOwner *mergeLineOwners(LineOwner *left, LineOwner *right,
int (*compare) (void const *a, void const *b))
{
LineOwner tmp;
LineOwner *np = &tmp;
tmp._link[LineOwner::Next] = np;
while(left && right)
{
if(compare(left, right) <= 0)
{
np->_link[LineOwner::Next] = left;
np = left;
left = &left->next();
}
else
{
np->_link[LineOwner::Next] = right;
np = right;
right = &right->next();
}
}
// At least one of these lists is now empty.
if(left)
{
np->_link[LineOwner::Next] = left;
}
if(right)
{
np->_link[LineOwner::Next] = right;
}
// Is the list empty?
if(!tmp.hasNext())
return NULL;
return &tmp.next();
}
static LineOwner *splitLineOwners(LineOwner *list)
{
if(!list) return NULL;
LineOwner *lista = list;
LineOwner *listb = list;
LineOwner *listc = list;
do
{
listc = listb;
listb = &listb->next();
lista = &lista->next();
if(lista)
{
lista = &lista->next();
}
} while(lista);
listc->_link[LineOwner::Next] = NULL;
return listb;
}
/**
* This routine uses a recursive mergesort algorithm; O(NlogN)
*/
static LineOwner *sortLineOwners(LineOwner *list,
int (*compare) (void const *a, void const *b))
{
if(list && list->_link[LineOwner::Next])
{
LineOwner *p = splitLineOwners(list);
// Sort both halves and merge them back.
list = mergeLineOwners(sortLineOwners(list, compare),
sortLineOwners(p, compare), compare);
}
return list;
}
static void setVertexLineOwner(Vertex *vtx, LineDef *lineptr, LineOwner **storage)
{
if(!lineptr) return;
// Has this line already been registered with this vertex?
LineOwner const *own = vtx->firstLineOwner();
while(own)
{
if(&own->line() == lineptr)
return; // Yes, we can exit.
own = &own->next();
}
// Add a new owner.
vtx->_numLineOwners++;
LineOwner *newOwner = (*storage)++;
newOwner->_line = lineptr;
newOwner->_link[LineOwner::Previous] = NULL;
// Link it in.
// NOTE: We don't bother linking everything at this stage since we'll
// be sorting the lists anyway. After which we'll finish the job by
// setting the prev and circular links.
// So, for now this is only linked singlely, forward.
newOwner->_link[LineOwner::Next] = vtx->_lineOwners;
vtx->_lineOwners = newOwner;
// Link the line to its respective owner node.
if(vtx == &lineptr->v1())
lineptr->_vo[FROM] = newOwner;
else
lineptr->_vo[TO] = newOwner;
}
#ifdef DENG2_DEBUG
/**
* Determines whether the specified vertex @a v has a correctly formed line
* owner ring.
*/
static bool vertexHasValidLineOwnerRing(Vertex &v)
{
LineOwner const *base = v.firstLineOwner();
LineOwner const *cur = base;
do
{
if(&cur->prev().next() != cur) return false;
if(&cur->next().prev() != cur) return false;
} while((cur = &cur->next()) != base);
return true;
}
#endif
/**
* Generates the line owner rings for each vertex. Each ring includes all
* the lines which the vertex belongs to sorted by angle, (the rings are
* arranged in clockwise order, east = 0).
*/
static void buildVertexLineOwnerRings()
{
LOG_AS("buildVertexLineOwnerRings");
/*
* Step 1: Find and link up all line owners.
*/
// We know how many vertex line owners we need (numLineDefs * 2).
LineOwner *lineOwners = (LineOwner *) Z_Malloc(sizeof(LineOwner) * editMap.lines.count() * 2, PU_MAPSTATIC, 0);
LineOwner *allocator = lineOwners;
foreach(LineDef *line, editMap.lines)
for(uint p = 0; p < 2; ++p)
{
setVertexLineOwner(&line->vertex(p), line, &allocator);
}
/*
* Step 2: Sort line owners of each vertex and finalize the rings.
*/
foreach(Vertex *v, editMap.vertexes)
{
if(!v->_numLineOwners) continue;
// Sort them; ordered clockwise by angle.
rootVtx = v;
v->_lineOwners = sortLineOwners(v->_lineOwners, lineAngleSorter);
// Finish the linking job and convert to relative angles.
// They are only singly linked atm, we need them to be doubly
// and circularly linked.
binangle_t firstAngle = v->_lineOwners->angle();
LineOwner *last = v->_lineOwners;
LineOwner *p = &last->next();
while(p)
{
p->_link[LineOwner::Previous] = last;
// Convert to a relative angle between last and this.
last->_angle = last->angle() - p->angle();
last = p;
p = &p->next();
}
last->_link[LineOwner::Next] = v->_lineOwners;
v->_lineOwners->_link[LineOwner::Previous] = last;
// Set the angle of the last owner.
last->_angle = last->angle() - firstAngle;
/*#ifdef DENG2_DEBUG
LOG_VERBOSE("Vertex #%i: line owners #%i")
<< editMap.vertexes.indexOf(v) << v->lineOwnerCount();
LineOwner const *base = v->firstLineOwner();
LineOwner const *cur = base;
uint idx = 0;
do
{
LOG_VERBOSE(" %i: p= #%05i this= #%05i n= #%05i, dANG= %-3.f")
<< idx << cur->prev().line().origIndex() << cur->line().origIndex()
<< cur->next().line().origIndex() << BANG2DEG(cur->angle());
idx++;
} while((cur = &cur->next()) != base);
#endif*/
// Sanity check.
DENG2_ASSERT(vertexHasValidLineOwnerRing(*v));
}
}
#if 0 /* Currently unused. */
/**
* @return The "lowest" vertex (normally the left-most, but if the line is vertical,
* then the bottom-most). @c => 0 for start, 1 for end.
*/
static inline int lineVertexLowest(LineDef const *l)
{
if(int( l->v[0]->buildData.pos[VX] ) < int( l->v[1]->buildData.pos[VX] ))
return 0;
return (int( l->v[0]->buildData.pos[VX] ) == int( l->v[1]->buildData.pos[VX] ) &&
int( l->v[0]->buildData.pos[VY] ) < int( l->v[1]->buildData.pos[VY] )? 0 : 1);
}
static int lineStartCompare(void const *p1, void const *p2)
{
LineDef const *a = (LineDef const *) p1;
LineDef const *b = (LineDef const *) p2;
// Determine left-most vertex of each line.
Vertex *c = (lineVertexLowest(a)? a->v[1] : a->v[0]);
Vertex *d = (lineVertexLowest(b)? b->v[1] : b->v[0]);
if(int( c->buildData.pos[VX] ) != int( d->buildData.pos[VX] ))
return int( c->buildData.pos[VX] ) - int( d->buildData.pos[VX] );
return int( c->buildData.pos[VY] ) - int( d->buildData.pos[VY] );
}
static int lineEndCompare(void const *p1, void const *p2)
{
LineDef const *a = (LineDef const *) p1;
LineDef const *b = (LineDef const *) p2;
// Determine right-most vertex of each line.
Vertex *c = (lineVertexLowest(a)? a->v[0] : a->v[1]);
Vertex *d = (lineVertexLowest(b)? b->v[0] : b->v[1]);
if(int( c->buildData.pos[VX] ) != int( d->buildData.pos[VX] ))
return int( c->buildData.pos[VX] ) - int( d->buildData.pos[VX] );
return int( c->buildData.pos[VY] ) - int( d->buildData.pos[VY] );
}
size_t numOverlaps;
int testOverlaps(LineDef *b, void *data)
{
LineDef *a = (LineDef *) data;
if(a != b)
{
if(lineStartCompare(a, b) == 0)
if(lineEndCompare(a, b) == 0)
{
// Found an overlap!
b->buildData.overlap = (a->buildData.overlap ? a->buildData.overlap : a);
numOverlaps++;
}
}
return false; // Continue iteration.
}
struct findoverlaps_params_t
{
GameMap *map;
uint coords[2]; // Blockmap cell coordinates.
};
static int findOverlapsForLine(LineDef *l, void *parameters)
{
findoverlaps_params_t *p = (findoverlaps_params_t *) parameters;
GameMap_IterateCellLineDefs(p->map, p->coords, testOverlaps, l);
return false; // Continue iteration.
}
/**
* @note Does not detect partially overlapping lines!
*/
void MPE_DetectOverlappingLines(GameMap *map)
{
numOverlaps = 0;
uint bmapDimensions[2];
Blockmap_Size(map->lineDefBlockmap, bmapDimensions);
findoverlaps_params_t parms;
for(uint y = 0; y < bmapSize[VY]; ++y)
for(uint x = 0; x < bmapSize[VX]; ++x)
{
parms.map = map;
parms.coords[VX] = x;
parms.coords[VY] = y;
GameMap_IterateCellLineDefs(map, params.coords, findOverlapsForLine, &parms);
}
if(numOverlaps == 0) return;
VERBOSE( Con_Message("Detected %lu overlapping linedefs.", (unsigned long) numOverlaps) )
}
#endif
GameMap *MPE_GetLastBuiltMap()
{
return lastBuiltMap;
}
bool MPE_GetLastBuiltMapResult()
{
return lastBuiltMapResult;
}
#undef MPE_Begin
boolean MPE_Begin(char const *mapUri)
{
/// @todo Do not ignore; assign to the editable map.
DENG_UNUSED(mapUri);
// Already been here?
if(editMapInited) return true;
// Initialize the game-specific map entity property database.
editMap.entityDatabase = EntityDatabase_New();
editMap.clear();
lastBuiltMap = 0;
lastBuiltMapResult = false; // Failed (default).
editMapInited = true;
return true;
}
#undef MPE_End
boolean MPE_End()
{
if(!editMapInited)
return false;
/*
* Log warnings about any issues we encountered during conversion of
* the basic map data elements.
*/
printMissingMaterialsInDict();
clearMaterialDict();
GameMap *map = new GameMap;
/*
* Perform cleanup on the loaded map data, removing duplicate vertexes,
* pruning unused sectors etc, etc...
*/
#if 0
markDuplicateVertexes(editMap);
pruneMapElements(editMap, PRUNE_ALL);
#endif
buildVertexLineOwnerRings();
/*
* Acquire ownership of the map elements from the editable map.
*/
map->entityDatabase = editMap.entityDatabase; // Take ownership.
editMap.entityDatabase = 0;
DENG2_ASSERT(map->_vertexes.isEmpty());
#ifdef DENG2_QT_4_7_OR_NEWER
map->_vertexes.reserve(editMap.vertexes.count());
#endif
while(!editMap.vertexes.isEmpty())
{
map->_vertexes.append(editMap.vertexes.takeFirst());
}
// Collate sectors:
DENG2_ASSERT(map->_sectors.isEmpty());
#ifdef DENG2_QT_4_7_OR_NEWER
map->_sectors.reserve(editMap.sectors.count());
#endif
while(!editMap.sectors.isEmpty())
{
map->_sectors.append(editMap.sectors.takeFirst());
}
// Collate sidedefs:
DENG2_ASSERT(map->_sideDefs.isEmpty());
#ifdef DENG2_QT_4_7_OR_NEWER
map->_sideDefs.reserve(editMap.sideDefs.count());
#endif
while(!editMap.sideDefs.isEmpty())
{
map->_sideDefs.append(editMap.sideDefs.takeFirst());
}
// Collate lines:
DENG2_ASSERT(map->_lines.isEmpty());
#ifdef DENG2_QT_4_7_OR_NEWER
map->_lines.reserve(editMap.lines.count());