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
|
#include "MapPositionManager.h"
#include "maplib.h"
#include "ientity.h"
#include "icameraview.h"
#include "gamelib.h"
#include "iregistry.h"
#include "itextstream.h"
#include "imapresource.h"
#include "icommandsystem.h"
#include "string/string.h"
#include "entitylib.h"
#include <functional>
#include "map/Map.h"
namespace map
{
namespace
{
const std::string SAVE_COMMAND_ROOT = "SavePosition";
const std::string LOAD_COMMAND_ROOT = "LoadPosition";
const char* const LAST_CAM_POSITION_KEY = "LastCameraPosition";
const char* const LAST_CAM_ANGLE_KEY = "LastCameraAngle";
const char* const GKEY_LAST_CAM_POSITION = "/mapFormat/lastCameraPositionKey";
const char* const GKEY_LAST_CAM_ANGLE = "/mapFormat/lastCameraAngleKey";
const char* const GKEY_PLAYER_START_ECLASS = "/mapFormat/playerStartPoint";
const char* const GKEY_PLAYER_HEIGHT = "/defaults/playerHeight";
const unsigned int MAX_POSITIONS = 10;
bool tryLoadLastPositionFromMapRoot(Vector3& origin, Vector3& angles)
{
auto mapRoot = GlobalMapModule().getRoot();
if (mapRoot)
{
const std::string savedOrigin = mapRoot->getProperty(LAST_CAM_POSITION_KEY);
if (!savedOrigin.empty())
{
// Construct the vector out of the std::string
origin = string::convert<Vector3>(savedOrigin);
angles = string::convert<Vector3>(mapRoot->getProperty(LAST_CAM_ANGLE_KEY));
return true;
}
}
return false;
}
bool tryLoadLastPositionFromWorldspawn(Vector3& origin, Vector3& angles)
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
// Try to find a saved "last camera position"
const std::string savedOrigin = worldspawn->getKeyValue(keyLastCamPos);
if (!savedOrigin.empty())
{
// Construct the vector out of the std::string
origin = string::convert<Vector3>(savedOrigin);
angles = string::convert<Vector3>(worldspawn->getKeyValue(keyLastCamAngle));
return true;
}
}
return false;
}
bool tryGetStartPositionFromPlayerStart(Vector3& origin, Vector3& angles)
{
// Get the player start entity
const std::string eClassPlayerStart = game::current::getValue<std::string>(GKEY_PLAYER_START_ECLASS);
Entity* playerStart = Scene_FindEntityByClass(eClassPlayerStart);
if (playerStart != nullptr)
{
// Get the entity origin
origin = string::convert<Vector3>(playerStart->getKeyValue("origin"));
// angua: move the camera upwards a bit
origin.z() += game::current::getValue<float>(GKEY_PLAYER_HEIGHT);
// Check for an angle key, and use it if present
angles[camera::CAMERA_YAW] = string::convert<float>(playerStart->getKeyValue("angle"), 0);
return true;
}
return false;
}
}
MapPositionManager::MapPositionManager()
{
_mapEventConn = GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &MapPositionManager::onMapEvent)
);
GlobalMapResourceManager().signal_onResourceExporting().connect(sigc::mem_fun(
this, &MapPositionManager::onPreMapExport
));
// Create the MapPosition objects and add the commands to the eventmanager
for (unsigned int i = 1; i <= MAX_POSITIONS; i++)
{
// Allocate a new MapPosition object and store the shared_ptr
_positions[i] = std::make_shared<MapPosition>(i);
// Add the load/save commands to the eventmanager and point it to the member
GlobalCommandSystem().addCommand(
SAVE_COMMAND_ROOT + string::to_string(i),
std::bind(&MapPosition::store, _positions[i].get(), std::placeholders::_1)
);
GlobalCommandSystem().addCommand(
LOAD_COMMAND_ROOT + string::to_string(i),
std::bind(&MapPosition::recall, _positions[i].get(), std::placeholders::_1)
);
}
}
MapPositionManager::~MapPositionManager()
{
_mapEventConn.disconnect();
}
void MapPositionManager::convertLegacyPositions()
{
Entity* worldspawn = map::current::getWorldspawn();
auto mapRoot = GlobalMapModule().getRoot();
if (worldspawn == nullptr || !mapRoot)
{
return; // no worldspawn or root
}
for (unsigned int i = 1; i <= MAX_POSITIONS; i++)
{
MapPosition pos(i);
pos.loadFrom(worldspawn);
if (!pos.empty() && mapRoot)
{
rMessage() << "Converting legacy map position #" << i << std::endl;
pos.saveTo(mapRoot);
pos.removeFrom(worldspawn);
}
}
}
void MapPositionManager::removeLegacyCameraPosition()
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
worldspawn->setKeyValue(keyLastCamPos, "");
worldspawn->setKeyValue(keyLastCamAngle, "");
}
}
void MapPositionManager::saveLastCameraPosition(const scene::IMapRootNodePtr& root)
{
if (!root)
{
return;
}
try
{
auto& camView = GlobalCameraManager().getActiveView();
root->setProperty(LAST_CAM_POSITION_KEY, string::to_string(camView.getCameraOrigin()));
root->setProperty(LAST_CAM_ANGLE_KEY, string::to_string(camView.getCameraAngles()));
}
catch (const std::runtime_error& ex)
{
rError() << "Cannot save last camera position: " << ex.what() << std::endl;
}
}
void MapPositionManager::gotoLastCameraPosition()
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
const std::string eClassPlayerStart = game::current::getValue<std::string>(GKEY_PLAYER_START_ECLASS);
Vector3 angles(0, 0, 0);
Vector3 origin(0, 0, 0);
if (tryLoadLastPositionFromMapRoot(origin, angles) ||
tryLoadLastPositionFromWorldspawn(origin, angles) ||
tryGetStartPositionFromPlayerStart(origin, angles))
{
// Focus the view with the given parameters
GlobalCommandSystem().executeCommand("FocusViews", cmd::ArgumentList{ origin, angles });
}
}
void MapPositionManager::loadMapPositions()
{
auto mapRoot = GlobalMapModule().getRoot();
if (!mapRoot)
{
return;
}
for (const auto& position : _positions)
{
position.second->loadFrom(mapRoot);
}
}
void MapPositionManager::clearPositions()
{
for (const auto& position : _positions)
{
position.second->clear();
}
}
void MapPositionManager::onMapEvent(IMap::MapEvent ev)
{
switch (ev)
{
case IMap::MapLoaded:
// Load legacy positions from the worldspawn entity
// and move them, from now on everything
// will be stored to the root node's property bag
convertLegacyPositions();
// After converting the legacy ones, load the positions
// from the map root, these take precedence
loadMapPositions();
gotoLastCameraPosition();
// Remove any legacy keyvalues from worldspawn
removeLegacyCameraPosition();
break;
case IMap::MapUnloaded:
clearPositions();
break;
default:
return;
};
}
void MapPositionManager::onPreMapExport(const scene::IMapRootNodePtr& root)
{
// Before any map is exported, save the last cam position to its root node
saveLastCameraPosition(root);
}
} // namespace map
|