Skip to content

Commit

Permalink
Refactor|libdoomsday: Querying whether map entity properties have values
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 18, 2016
1 parent e68955a commit c38b28c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
Expand Up @@ -64,6 +64,8 @@ class LIBDOOMSDAY_PUBLIC EntityDatabase
*/
PropertyValue const &property(MapEntityPropertyDef const *def, int elementIndex) const;

bool hasPropertyValue(MapEntityPropertyDef const *def, int elementIndex) const;

/**
* Replace/add a value for a known entity element property to the database.
*
Expand Down
4 changes: 3 additions & 1 deletion doomsday/apps/libdoomsday/include/doomsday/world/entitydef.h
Expand Up @@ -115,7 +115,7 @@ LIBDOOMSDAY_PUBLIC MapEntityDef *P_MapEntityDefByName(char const *name);
*
* @return Unique name associated with @a def if found, else a zero-length string.
*/
LIBDOOMSDAY_PUBLIC AutoStr *P_NameForMapEntityDef(MapEntityDef *def);
LIBDOOMSDAY_PUBLIC AutoStr *P_NameForMapEntityDef(MapEntityDef const *def);

/**
* To be called to initialize the game map object defs.
Expand All @@ -138,6 +138,8 @@ LIBDOOMSDAY_PUBLIC angle_t P_GetGMOAngle(int entityId, int elementIndex, int pr
LIBDOOMSDAY_PUBLIC float P_GetGMOFloat(int entityId, int elementIndex, int propertyId);
LIBDOOMSDAY_PUBLIC double P_GetGMODouble(int entityId, int elementIndex, int propertyId);

LIBDOOMSDAY_PUBLIC dd_bool P_GMOPropertyIsSet(int entityId, int elementIndex, int propertyId);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
48 changes: 34 additions & 14 deletions doomsday/apps/libdoomsday/src/world/entitydatabase.cpp
Expand Up @@ -80,6 +80,25 @@ DENG2_PIMPL(EntityDatabase)
result = set.insert(std::pair<int, Entity>(elementIndex, Entity()));
return &result.first->second;
}

PropertyValue const *tryFindPropertyValue(MapEntityPropertyDef const &def,
int elementIndex)
{
Entities *set = entities(def.entity->id);
Entity const *entity = entityByElementIndex(*set, elementIndex, false /*do not create*/);
if (!entity)
{
throw Error("EntityDatabase::property", QString("There is no element %1 of type %2")
.arg(elementIndex)
.arg(Str_Text(P_NameForMapEntityDef(def.entity))));
}
Entity::const_iterator found = entity->find(def.id);
if (found == entity->end())
{
return nullptr;
}
return found->second;
}
};

EntityDatabase::EntityDatabase() : d(new Impl(this))
Expand All @@ -103,20 +122,21 @@ PropertyValue const &EntityDatabase::property(MapEntityPropertyDef const *def,
int elementIndex) const
{
DENG2_ASSERT(def);
Entities *set = d->entities(def->entity->id);
Entity *entity = d->entityByElementIndex(*set, elementIndex, false /*do not create*/);
if (!entity)
throw Error("EntityDatabase::property", QString("There is no element %1 of type %2")
.arg(elementIndex)
.arg(Str_Text(P_NameForMapEntityDef(def->entity))));

Entity::const_iterator found = entity->find(def->id);
if (found == entity->end())
throw Error("EntityDatabase::property", QString("Element %1 of type %2 has no value for property %3")
.arg(elementIndex)
.arg(Str_Text(P_NameForMapEntityDef(def->entity)))
.arg(def->id));
return *found->second;
if (auto const *propValue = d->tryFindPropertyValue(*def, elementIndex))
{
return *propValue;
}
throw Error("EntityDatabase::property",
QString("Element %1 of type %2 has no value for property %3")
.arg(elementIndex)
.arg(Str_Text(P_NameForMapEntityDef(def->entity)))
.arg(def->id));
}

bool EntityDatabase::hasPropertyValue(MapEntityPropertyDef const *def, int elementIndex) const
{
DENG2_ASSERT(def);
return d->tryFindPropertyValue(*def, elementIndex) != nullptr;
}

void EntityDatabase::setProperty(MapEntityPropertyDef const *def, int elementIndex,
Expand Down
12 changes: 11 additions & 1 deletion doomsday/apps/libdoomsday/src/world/entitydef.cpp
Expand Up @@ -83,7 +83,7 @@ MapEntityDef *P_MapEntityDefByName(char const *name)
return nullptr; // Not found.
}

AutoStr *P_NameForMapEntityDef(MapEntityDef *def)
AutoStr *P_NameForMapEntityDef(MapEntityDef const *def)
{
String name; // Not found.
if (def)
Expand Down Expand Up @@ -312,6 +312,16 @@ static void setValue(void *dst, valuetype_t dstType, PropertyValue const &pvalue
}
}

dd_bool P_GMOPropertyIsSet(int entityId, int elementIndex, int propertyId)
{
if (World::get().hasMap())
{
World::get().map().entityDatabase()
.hasPropertyValue(entityPropertyDef(entityId, propertyId), elementIndex);
}
return false;
}

template <typename Type, valuetype_t returnValueType>
Type getEntityValue(int entityId, int elementIndex, int propertyId)
{
Expand Down

0 comments on commit c38b28c

Please sign in to comment.