/
api_mapedit.cpp
430 lines (360 loc) · 12.5 KB
/
api_mapedit.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
/** @file api_mapedit.cpp Internal runtime map editing interface.
*
* @authors Copyright © 2007-2015 Daniel Swanson <danij@dengine.net>
*
* @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 "de_platform.h"
#include "api_mapedit.h"
#include "world/map.h"
#include "world/polyobjdata.h"
#include "Plane"
#include "Sector"
#include "Surface"
#include "edit_map.h"
#include "dd_main.h"
#include <doomsday/world/entitydef.h>
#include <doomsday/world/Materials>
#include <doomsday/EntityDatabase>
#include <de/Error>
#include <de/Log>
#include <de/StringPool>
using namespace de;
using namespace world;
#define ERROR_IF_NOT_INITIALIZED() { \
if(!editMapInited) \
throw Error(QString("%s").arg(__FUNCTION__), "Not active, did you forget to call MPE_Begin()?"); \
}
static Map *editMap;
static bool editMapInited;
/**
* 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;
/**
* 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()
{
if(!::materialDict) return;
::materialDict->forAll([] (StringPool::Id id)
{
// A valid id?
if(::materialDict->string(id))
{
// An unresolved reference?
if(!::materialDict->userPointer(id))
{
LOG_RES_WARNING("Found %4i x unknown material \"%s\"")
<< ::materialDict->userValue(id)
<< ::materialDict->string(id);
}
}
return LoopContinue;
});
}
/**
* 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(const String &materialUriStr)
{
if(materialUriStr.isEmpty()) return 0;
// Time to create the dictionary?
if(!materialDict)
{
materialDict = new StringPool;
}
de::Uri materialUri(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 = &world::Materials::get().material(materialUri);
}
catch(Resources::MissingResourceManifestError const &)
{
// Try any scheme.
try
{
materialUri.setScheme("");
material = &world::Materials::get().material(materialUri);
}
catch(Resources::MissingResourceManifestError 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;
}
static inline Material *findMaterialInDict(const char *materialUriStr)
{
if (!materialUriStr) return nullptr;
return findMaterialInDict(String(materialUriStr));
}
Map *MPE_Map()
{
return editMapInited? editMap : 0;
}
Map *MPE_TakeMap()
{
editMapInited = false;
Map *retMap = editMap; editMap = 0;
return retMap;
}
#undef MPE_Begin
dd_bool MPE_Begin(uri_s const * /*mapUri*/)
{
if(!editMapInited)
{
delete editMap;
editMap = new Map;
editMapInited = true;
}
return true;
}
#undef MPE_End
dd_bool MPE_End()
{
if(!editMapInited)
return false;
/*
* Log warnings about any issues we encountered during conversion of
* the basic map data elements.
*/
printMissingMaterialsInDict();
clearMaterialDict();
// Note the map is left in an editable state in case the caller decides
// they aren't finished after all...
return true;
}
#undef MPE_VertexCreate
int MPE_VertexCreate(coord_t x, coord_t y, int archiveIndex)
{
ERROR_IF_NOT_INITIALIZED();
return editMap->createVertex(Vector2d(x, y), archiveIndex)->indexInMap();
}
#undef MPE_VertexCreatev
dd_bool MPE_VertexCreatev(int num, coord_t const *values, int *archiveIndices, int *retIndices)
{
ERROR_IF_NOT_INITIALIZED();
if(num <= 0 || !values)
return false;
// Create many vertexes.
for(int n = 0; n < num; ++n)
{
Vertex *vertex = editMap->createVertex(Vector2d(values[n * 2], values[n * 2 + 1]),
archiveIndices[n]);
if(retIndices)
{
retIndices[n] = vertex->indexInMap();
}
}
return true;
}
#undef MPE_LineCreate
int MPE_LineCreate(int v1, int v2, int frontSectorIdx, int backSectorIdx, int flags,
int archiveIndex)
{
ERROR_IF_NOT_INITIALIZED();
if(frontSectorIdx >= editMap->editableSectorCount()) return -1;
if(backSectorIdx >= editMap->editableSectorCount()) return -1;
if(v1 < 0 || v1 >= editMap->vertexCount()) return -1;
if(v2 < 0 || v2 >= editMap->vertexCount()) return -1;
if(v1 == v2) return -1;
// Next, check the length is not zero.
/// @todo fixme: We need to allow these... -ds
Vertex &vtx1 = editMap->vertex(v1);
Vertex &vtx2 = editMap->vertex(v2);
if(de::abs(Vector2d(vtx1.origin() - vtx2.origin()).length()) <= 0.0001) return -1;
Sector *frontSector = (frontSectorIdx >= 0? editMap->editableSectors().at(frontSectorIdx) : 0);
Sector *backSector = (backSectorIdx >= 0? editMap->editableSectors().at(backSectorIdx) : 0);
return editMap->createLine(vtx1, vtx2, flags, frontSector, backSector, archiveIndex)
->indexInMap();
}
#undef MPE_LineAddSide
void MPE_LineAddSide(int lineIdx, int sideId, short flags, const char *topMaterialUri,
float topOffsetX, float topOffsetY, float topRed, float topGreen,
float topBlue, const char *middleMaterialUri, float middleOffsetX,
float middleOffsetY, float middleRed, float middleGreen, float middleBlue,
float middleOpacity, const char *bottomMaterialUri, float bottomOffsetX,
float bottomOffsetY, float bottomRed, float bottomGreen, float bottomBlue,
int archiveIndex)
{
ERROR_IF_NOT_INITIALIZED();
if(lineIdx < 0 || lineIdx >= editMap->editableLineCount()) return;
Line *line = editMap->editableLines().at(lineIdx);
LineSide &side = line->side(sideId);
side.setFlags(flags);
side.setIndexInArchive(archiveIndex);
// Ensure sections are defined if they aren't already.
side.addSections();
// Assign the resolved material if found.
side.top()
.setMaterial(findMaterialInDict(topMaterialUri))
.setOrigin(Vector2f(topOffsetX, topOffsetY))
.setColor(Vector3f(topRed, topGreen, topBlue));
side.middle()
.setMaterial(findMaterialInDict(middleMaterialUri))
.setOrigin(Vector2f(middleOffsetX, middleOffsetY))
.setColor(Vector3f(middleRed, middleGreen, middleBlue))
.setOpacity(middleOpacity);
side.bottom()
.setMaterial(findMaterialInDict(bottomMaterialUri))
.setOrigin(Vector2f(bottomOffsetX, bottomOffsetY))
.setColor(Vector3f(bottomRed, bottomGreen, bottomBlue));
}
#undef MPE_PlaneCreate
int MPE_PlaneCreate(int sectorIdx, coord_t height, const char *materialUri,
float matOffsetX, float matOffsetY, float tintRed, float tintGreen, float tintBlue, float opacity,
float normalX, float normalY, float normalZ, int archiveIndex)
{
ERROR_IF_NOT_INITIALIZED();
if(sectorIdx < 0 || sectorIdx >= editMap->editableSectorCount()) return -1;
Sector *sector = editMap->editableSectors().at(sectorIdx);
Plane *plane = sector->addPlane(Vector3f(normalX, normalY, normalZ), height);
plane->setIndexInArchive(archiveIndex);
plane->surface()
.setMaterial(findMaterialInDict(materialUri))
.setColor(Vector3f(tintRed, tintGreen, tintBlue))
.setOrigin(Vector2f(matOffsetX, matOffsetY));
if(!plane->isSectorFloor() && !plane->isSectorCeiling())
{
plane->surface().setOpacity(opacity);
}
return plane->indexInSector();
}
#undef MPE_SectorCreate
int MPE_SectorCreate(float lightlevel, float red, float green, float blue,
int archiveIndex)
{
ERROR_IF_NOT_INITIALIZED();
return editMap->createSector(lightlevel, Vector3f(red, green, blue),
archiveIndex)->indexInMap();
}
#undef MPE_PolyobjCreate
int MPE_PolyobjCreate(int const *lines, int lineCount, int tag, int sequenceType,
coord_t originX, coord_t originY, int archiveIndex)
{
DENG_UNUSED(archiveIndex); /// @todo Use this!
ERROR_IF_NOT_INITIALIZED();
if(lineCount <= 0 || !lines) return -1;
// First check that all the line indices are valid and that they arn't
// already part of another polyobj.
for(int i = 0; i < lineCount; ++i)
{
if(lines[i] < 0 || lines[i] >= editMap->editableLineCount()) return -1;
Line *line = editMap->editableLines().at(lines[i]);
if(line->definesPolyobj()) return -1;
}
Polyobj *po = editMap->createPolyobj(Vector2d(originX, originY));
po->setSequenceType(sequenceType);
po->setTag(tag);
for(int i = 0; i < lineCount; ++i)
{
Line *line = editMap->editableLines().at(lines[i]);
// This line belongs to a polyobj.
line->setPolyobj(po);
po->data().lines.append(line);
}
return po->indexInMap();
}
#undef MPE_GameObjProperty
dd_bool MPE_GameObjProperty(char const *entityName, int elementIndex,
char const *propertyName, valuetype_t valueType, void *valueAdr)
{
LOG_AS("MPE_GameObjProperty");
ERROR_IF_NOT_INITIALIZED();
if(!entityName || !propertyName || !valueAdr)
return false; // Hmm...
// Is this a known entity?
MapEntityDef *entityDef = P_MapEntityDefByName(entityName);
if(!entityDef)
{
LOG_WARNING("Unknown entity name:\"%s\", ignoring.") << entityName;
return false;
}
// Is this a known property?
MapEntityPropertyDef *propertyDef;
if(MapEntityDef_PropertyByName(entityDef, propertyName, &propertyDef) < 0)
{
LOG_WARNING("Entity \"%s\" has no \"%s\" property, ignoring.")
<< entityName << propertyName;
return false;
}
try
{
EntityDatabase &entities = editMap->entityDatabase();
entities.setProperty(propertyDef, elementIndex, valueType, valueAdr);
return true;
}
catch(Error const &er)
{
LOG_WARNING("%s. Ignoring.") << er.asText();
}
return false;
}
DENG_DECLARE_API(MPE) =
{
{ DE_API_MAP_EDIT },
MPE_Begin,
MPE_End,
MPE_VertexCreate,
MPE_VertexCreatev,
MPE_LineCreate,
MPE_LineAddSide,
MPE_SectorCreate,
MPE_PlaneCreate,
MPE_PolyobjCreate,
MPE_GameObjProperty
};