/
p_data.cpp
444 lines (387 loc) · 13.4 KB
/
p_data.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
/** @file p_data.cpp World playsim data structures.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is M_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>
*/
#include <cmath>
#include <map>
#include <de/memory.h>
#include <de/Error>
#include <de/Log>
#include <de/String>
#include <de/StringPool>
#include "de_base.h"
#include "de_play.h"
#include "EntityDatabase"
#include "world/map.h"
#include "world/propertyvalue.h"
using namespace de;
// Map entity definitions.
static StringPool *entityDefs;
typedef std::map<int, StringPool::Id> EntityDefIdMap;
static EntityDefIdMap entityDefIdMap;
static int clearEntityDefsWorker(StringPool::Id id, void * /*parameters*/)
{
MapEntityDef *def = static_cast<MapEntityDef *>( entityDefs->userPointer(id) );
DENG2_ASSERT(def);
for(uint i = 0; i < def->numProps; ++i)
{
MapEntityPropertyDef *prop = def->props + i;
M_Free(prop->name);
}
M_Free(def->props);
delete def;
return false; // Continue iteration.
}
static void clearEntityDefs(void)
{
if(!entityDefs) return;
entityDefs->iterate(clearEntityDefsWorker, 0/*no parameters*/);
delete entityDefs; entityDefs = 0;
entityDefIdMap.clear();
}
MapEntityDef *P_MapEntityDef(int id)
{
EntityDefIdMap::iterator i = entityDefIdMap.find(id);
if(i != entityDefIdMap.end())
{
StringPool::Id id = i->second;
return static_cast<MapEntityDef *>( entityDefs->userPointer(id) );
}
return 0; // Not found.
}
MapEntityDef *P_MapEntityDefByName(char const *name)
{
if(name && entityDefs)
{
StringPool::Id id = entityDefs->isInterned(String(name));
return static_cast<MapEntityDef *>( entityDefs->userPointer(id) );
}
return 0; // Not found.
}
static int P_NameForMapEntityDefWorker(StringPool::Id id, void *parameters)
{
MapEntityDef *def = static_cast<MapEntityDef *>( parameters );
if(entityDefs->userPointer(id) == def) return id;
return 0; // Continue iteration.
}
AutoStr *P_NameForMapEntityDef(MapEntityDef *def)
{
if(def)
{
StringPool::Id id = entityDefs->iterate(P_NameForMapEntityDefWorker, def);
String const& name = entityDefs->string(id);
QByteArray nameUtf8 = name.toUtf8();
return AutoStr_FromText(nameUtf8.constData());
}
return AutoStr_NewStd();
}
int MapEntityDef_Property(MapEntityDef *def, int propertyId,
MapEntityPropertyDef **retDef = 0)
{
DENG2_ASSERT(def);
MapEntityPropertyDef *found = 0;
for(uint i = 0; i < def->numProps; ++i)
{
MapEntityPropertyDef *prop = def->props + i;
if(prop->id == propertyId)
{
found = prop;
break;
}
}
if(retDef) *retDef = found;
return found? found - def->props : -1/* not found */;
}
int MapEntityDef_PropertyByName(MapEntityDef *def, char const *propertyName,
MapEntityPropertyDef **retDef)
{
DENG2_ASSERT(def);
MapEntityPropertyDef *found = 0;
if(propertyName && propertyName[0])
{
for(uint i = 0; i < def->numProps; ++i)
{
MapEntityPropertyDef *prop = def->props + i;
if(!stricmp(prop->name, propertyName))
{
found = prop;
break;
}
}
}
if(retDef) *retDef = found;
return found? found - def->props : -1/* not found */;
}
void MapEntityDef_AddProperty(MapEntityDef* def, int propertyId, const char* propertyName,
valuetype_t type)
{
DENG2_ASSERT(def);
if(propertyId == 0) // Not a valid identifier.
throw Error("MapEntityDef_AddProperty", "0 is not a valid propertyId");
if(!propertyName || !propertyName[0]) // Must have a name.
throw Error("MapEntityDef_AddProperty", "Invalid propertyName (zero-length string)");
// A supported value type?
switch(type)
{
case DDVT_BYTE:
case DDVT_SHORT:
case DDVT_INT:
case DDVT_FIXED:
case DDVT_ANGLE:
case DDVT_FLOAT:
break;
default:
throw Error("MapEntityDef_AddProperty", QString("Unknown/not supported value type %1").arg(type));
}
// Ensure both the identifer and the name for the new property are unique.
if(MapEntityDef_Property(def, propertyId) >= 0)
throw Error("MapEntityDef_AddProperty", QString("propertyId %1 not unique for %2")
.arg(propertyId).arg(Str_Text(P_NameForMapEntityDef(def))));
if(MapEntityDef_PropertyByName(def, propertyName) >= 0)
throw Error("MapEntityDef_AddProperty", QString("propertyName \"%1\" not unique for %2")
.arg(propertyName).arg(Str_Text(P_NameForMapEntityDef(def))));
// Looks good! Add it to the list of properties.
def->props = (MapEntityPropertyDef*) M_Realloc(def->props, ++def->numProps * sizeof(*def->props));
if(!def->props)
throw Error("MapEntityDef_AddProperty",
QString("Failed on (re)allocation of %1 bytes for new MapEntityPropertyDef array")
.arg((unsigned long) sizeof(*def->props)));
MapEntityPropertyDef* prop = &def->props[def->numProps - 1];
prop->id = propertyId;
int len = (int)strlen(propertyName);
prop->name = (char *) M_Malloc(sizeof(*prop->name) * (len + 1));
if(!prop->name)
throw Error("MapEntityDef_AddProperty",
QString("Failed on allocation of %1 bytes for property name")
.arg((unsigned long) (sizeof(*prop->name) * (len + 1))));
strncpy(prop->name, propertyName, len);
prop->name[len] = '\0';
prop->type = type;
prop->entity = def;
}
/**
* Look up a mapobj definition.
*
* @param identifer If objName is @c NULL, compare using this unique identifier.
* @param entityName If not @c NULL, compare using this unique name.
* @param canCreate @c true= create a new definition if not found.
*/
static MapEntityDef *findMapEntityDef(int identifier, char const *entityName,
bool canCreate)
{
if(identifier == 0 && (!entityName || !entityName[0])) return 0;
// Is this an already known entity?
if(entityName && entityName[0])
{
MapEntityDef *found = P_MapEntityDefByName(entityName);
if(found) return found;
}
else
{
MapEntityDef *found = P_MapEntityDef(identifier);
if(found) return found;
}
// An unknown entity. Are we creating?
if(!canCreate) return 0;
// Ensure the name is unique.
if(P_MapEntityDefByName(entityName)) return 0;
// Ensure the identifier is unique.
if(P_MapEntityDef(identifier)) return 0;
// Have we yet to initialize the map entity definition dataset?
if(!entityDefs)
{
entityDefs = new StringPool;
}
StringPool::Id id = entityDefs->intern(String(entityName));
MapEntityDef *def = new MapEntityDef(identifier);
entityDefs->setUserPointer(id, def);
entityDefIdMap.insert(std::pair<int, StringPool::Id>(identifier, id));
return def;
}
#undef P_RegisterMapObj
DENG_EXTERN_C boolean P_RegisterMapObj(int identifier, char const *name)
{
return findMapEntityDef(identifier, name, true /*do create*/) != 0;
}
#undef P_RegisterMapObjProperty
DENG_EXTERN_C boolean P_RegisterMapObjProperty(int entityId, int propertyId,
char const *propertyName, valuetype_t type)
{
try
{
MapEntityDef *def = findMapEntityDef(entityId, 0, false /*do not create*/);
if(!def) throw Error("P_RegisterMapObjProperty", QString("Unknown entityId %1").arg(entityId));
MapEntityDef_AddProperty(def, propertyId, propertyName, type);
return true; // Success!
}
catch(Error const &er)
{
LOG_WARNING("%s. Ignoring.") << er.asText();
}
return false;
}
void P_InitMapEntityDefs()
{
// Allow re-init.
clearEntityDefs();
}
void P_ShutdownMapEntityDefs()
{
clearEntityDefs();
}
static MapEntityPropertyDef *entityPropertyDef(int entityId, int propertyId)
{
// Is this a known entity?
MapEntityDef *entity = P_MapEntityDef(entityId);
if(!entity) throw Error("entityPropertyDef", QString("Unknown entity definition id %1").arg(entityId));
// Is this a known property?
MapEntityPropertyDef *property;
if(MapEntityDef_Property(entity, propertyId, &property) < 0)
throw Error("entityPropertyDef", QString("Entity definition %1 has no property with id %2")
.arg(Str_Text(P_NameForMapEntityDef(entity)))
.arg(propertyId));
return property; // Found it.
}
static void setValue(void *dst, valuetype_t dstType, PropertyValue const &pvalue)
{
switch(dstType)
{
case DDVT_FIXED: *((fixed_t *) dst) = pvalue.asFixed(); break;
case DDVT_FLOAT: *( (float *) dst) = pvalue.asFloat(); break;
case DDVT_BYTE: *( (byte *) dst) = pvalue.asByte(); break;
case DDVT_INT: *( (int *) dst) = pvalue.asInt32(); break;
case DDVT_SHORT: *( (short *) dst) = pvalue.asInt16(); break;
case DDVT_ANGLE: *((angle_t *) dst) = pvalue.asAngle(); break;
default:
throw Error("setValue", QString("Unknown value type %d").arg(dstType));
}
}
#undef P_GetGMOByte
DENG_EXTERN_C byte P_GetGMOByte(int entityId, int elementIndex, int propertyId)
{
byte returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_BYTE, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}
#undef P_GetGMOShort
DENG_EXTERN_C short P_GetGMOShort(int entityId, int elementIndex, int propertyId)
{
short returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_SHORT, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}
#undef P_GetGMOInt
DENG_EXTERN_C int P_GetGMOInt(int entityId, int elementIndex, int propertyId)
{
int returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_INT, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}
#undef P_GetGMOFixed
DENG_EXTERN_C fixed_t P_GetGMOFixed(int entityId, int elementIndex, int propertyId)
{
fixed_t returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_FIXED, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}
#undef P_GetGMOAngle
DENG_EXTERN_C angle_t P_GetGMOAngle(int entityId, int elementIndex, int propertyId)
{
angle_t returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_ANGLE, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}
#undef P_GetGMOFloat
DENG_EXTERN_C float P_GetGMOFloat(int entityId, int elementIndex, int propertyId)
{
float returnVal = 0;
if(App_World().hasMap())
{
try
{
EntityDatabase &db = App_World().map().entityDatabase();
MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId);
setValue(&returnVal, DDVT_FLOAT, db.property(propDef, elementIndex));
}
catch(Error const &er)
{
LOG_WARNING("%s. Returning 0.") << er.asText();
}
}
return returnVal;
}