Skip to content

Commit

Permalink
Introduce a new convenience function to obtain the worldspawn
Browse files Browse the repository at this point in the history
Locating the worldspawn node and converting it into an Entity pointer is a very
common operation, but so far has had to be done manually each time. Now there
is a convenience function map::current::getWorldspawn() which does this
automatically.
  • Loading branch information
Matthew Mott committed Mar 25, 2020
1 parent 70dc070 commit 41cf59f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/imap.h
Expand Up @@ -114,3 +114,4 @@ inline IMap& GlobalMapModule() {
);
return _mapModule;
}

20 changes: 20 additions & 0 deletions libs/maplib.h
@@ -0,0 +1,20 @@
#pragma once

#include "imap.h"
#include "ientity.h"

namespace map
{

namespace current
{

/// Convenience method to return the worldspawn entity pointer
inline Entity* getWorldspawn()
{
return Node_getEntity(GlobalMapModule().getWorldspawn());
}

}

}
3 changes: 2 additions & 1 deletion plugins/dm.difficulty/DifficultySettingsManager.cpp
Expand Up @@ -4,6 +4,7 @@
#include "itextstream.h"
#include "entitylib.h"
#include "gamelib.h"
#include "maplib.h"
#include "string/string.h"
#include "registry/registry.h"
#include "DifficultyEntity.h"
Expand Down Expand Up @@ -71,7 +72,7 @@ void DifficultySettingsManager::loadMapSettings() {
void DifficultySettingsManager::loadDifficultyNames()
{
// Locate the worldspawn entity
Entity* worldspawn = Scene_FindEntityByClass("worldspawn");
Entity* worldspawn = map::current::getWorldspawn();

// Try to locate the difficulty menu entity, where the default names are defined
IEntityClassPtr menuEclass = GlobalEntityClassManager().findClass(
Expand Down
1 change: 1 addition & 0 deletions radiant/map/Map.h
Expand Up @@ -23,6 +23,7 @@ namespace map
class MapPositionManager;
class StartupMapLoader;

/// Main class representing the current map
class Map :
public IMap,
public scene::Graph::Observer
Expand Down
21 changes: 4 additions & 17 deletions radiant/map/MapPositionManager.cpp
@@ -1,5 +1,6 @@
#include "MapPositionManager.h"

#include "maplib.h"
#include "ientity.h"
#include "ieventmanager.h"
#include "iregistry.h"
Expand Down Expand Up @@ -56,12 +57,7 @@ MapPositionManager::MapPositionManager()

void MapPositionManager::loadPositions()
{
// Find the worldspawn node
const scene::INodePtr& wsNode = GlobalMapModule().getWorldspawn();

if (!wsNode) return;

Entity* worldspawn = Node_getEntity(wsNode);
Entity* worldspawn = map::current::getWorldspawn();

if (worldspawn != nullptr)
{
Expand All @@ -81,12 +77,7 @@ void MapPositionManager::loadPositions()

void MapPositionManager::savePositions()
{
// Find the worldspawn node
const scene::INodePtr& wsNode = GlobalMapModule().getWorldspawn();

if (!wsNode) return;

Entity* worldspawn = Node_getEntity(wsNode);
Entity* worldspawn = map::current::getWorldspawn();

for (unsigned int i = 1; i <= MAX_POSITIONS; ++i)
{
Expand All @@ -100,11 +91,7 @@ void MapPositionManager::savePositions()
void MapPositionManager::removePositions()
{
// Find the worldspawn node
const scene::INodePtr& wsNode = GlobalMapModule().getWorldspawn();

if (!wsNode) return;

Entity* worldspawn = Node_getEntity(wsNode);
Entity* worldspawn = map::current::getWorldspawn();

for (unsigned int i = 1; i <= MAX_POSITIONS; ++i)
{
Expand Down
10 changes: 6 additions & 4 deletions radiant/xyview/XYWnd.cpp
Expand Up @@ -34,6 +34,7 @@
#include "XYRenderer.h"
#include "gamelib.h"
#include "scenelib.h"
#include "maplib.h"
#include "render/frontend/RenderableCollectionWalker.h"

#include <fmt/format.h>
Expand Down Expand Up @@ -939,14 +940,15 @@ void XYWnd::drawGrid()

void XYWnd::drawBlockGrid()
{
if (GlobalMap().getWorldspawn() == NULL) {
return; // no worldspawn yet
}
// Do nothing if there is no worldspawn yet
Entity* worldSpawn = map::current::getWorldspawn();
if (!worldSpawn)
return;

// Set a default blocksize of 1024
int blockSize = GlobalXYWnd().defaultBlockSize();

// Check the worldspawn for a custom blocksize
Entity* worldSpawn = Node_getEntity(GlobalMap().getWorldspawn());
assert(worldSpawn);
std::string sizeVal = worldSpawn->getKeyValue("_blocksize");

Expand Down

0 comments on commit 41cf59f

Please sign in to comment.