Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/graphics/MapReader.cpp
Show First 20 Lines • Show All 365 Lines • ▼ Show 20 Lines | XERO_ITER_EL(root, child) | ||||
} | } | ||||
} | } | ||||
return PSRETURN_OK; | return PSRETURN_OK; | ||||
} | } | ||||
void CMapSummaryReader::GetMapSettings(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | void CMapSummaryReader::GetMapSettings(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
ScriptInterface::CreateObject(cx, ret); | ScriptInterface::CreateObject(rq, ret); | ||||
if (m_ScriptSettings.empty()) | if (m_ScriptSettings.empty()) | ||||
return; | return; | ||||
JS::RootedValue scriptSettingsVal(cx); | JS::RootedValue scriptSettingsVal(rq.cx); | ||||
scriptInterface.ParseJSON(m_ScriptSettings, &scriptSettingsVal); | scriptInterface.ParseJSON(m_ScriptSettings, &scriptSettingsVal); | ||||
scriptInterface.SetProperty(ret, "settings", scriptSettingsVal, false); | scriptInterface.SetProperty(ret, "settings", scriptSettingsVal, false); | ||||
} | } | ||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
▲ Show 20 Lines • Show All 871 Lines • ▼ Show 20 Lines | int CMapReader::LoadRMSettings() | ||||
ENSURE(pSimulation2); | ENSURE(pSimulation2); | ||||
pSimulation2->SetMapSettings(m_ScriptSettings); | pSimulation2->SetMapSettings(m_ScriptSettings); | ||||
return 0; | return 0; | ||||
} | } | ||||
int CMapReader::GenerateMap() | int CMapReader::GenerateMap() | ||||
{ | { | ||||
JSContext* cx = pSimulation2->GetScriptInterface().GetContext(); | ScriptInterface::Request rq(pSimulation2->GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
if (!m_MapGen) | if (!m_MapGen) | ||||
{ | { | ||||
// Initialize map generator | // Initialize map generator | ||||
m_MapGen = new CMapGenerator(); | m_MapGen = new CMapGenerator(); | ||||
VfsPath scriptPath; | VfsPath scriptPath; | ||||
Show All 15 Lines | if (progress < 0) | ||||
throw PSERROR_Game_World_MapLoadFailed("Error generating random map.\nCheck application log for details."); | throw PSERROR_Game_World_MapLoadFailed("Error generating random map.\nCheck application log for details."); | ||||
} | } | ||||
else if (progress == 0) | else if (progress == 0) | ||||
{ | { | ||||
// Finished, get results as StructuredClone object, which must be read to obtain the JS::Value | // Finished, get results as StructuredClone object, which must be read to obtain the JS::Value | ||||
shared_ptr<ScriptInterface::StructuredClone> results = m_MapGen->GetResults(); | shared_ptr<ScriptInterface::StructuredClone> results = m_MapGen->GetResults(); | ||||
// Parse data into simulation context | // Parse data into simulation context | ||||
JS::RootedValue data(cx); | JS::RootedValue data(rq.cx); | ||||
pSimulation2->GetScriptInterface().ReadStructuredClone(results, &data); | pSimulation2->GetScriptInterface().ReadStructuredClone(results, &data); | ||||
if (data.isUndefined()) | if (data.isUndefined()) | ||||
{ | { | ||||
// RMS failed - return to main menu | // RMS failed - return to main menu | ||||
throw PSERROR_Game_World_MapLoadFailed("Error generating random map.\nCheck application log for details."); | throw PSERROR_Game_World_MapLoadFailed("Error generating random map.\nCheck application log for details."); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
m_MapData.init(cx, data); | m_MapData.init(rq.cx, data); | ||||
} | } | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
// Still working | // Still working | ||||
// Sleep for a while, slowing down the rendering thread | // Sleep for a while, slowing down the rendering thread | ||||
// to allow more CPU for the map generator thread | // to allow more CPU for the map generator thread | ||||
SDL_Delay(100); | SDL_Delay(100); | ||||
} | } | ||||
// return progress | // return progress | ||||
return progress; | return progress; | ||||
}; | }; | ||||
int CMapReader::ParseTerrain() | int CMapReader::ParseTerrain() | ||||
{ | { | ||||
TIMER(L"ParseTerrain"); | TIMER(L"ParseTerrain"); | ||||
JSContext* cx = pSimulation2->GetScriptInterface().GetContext(); | ScriptInterface::Request rq(pSimulation2->GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
// parse terrain from map data | // parse terrain from map data | ||||
// an error here should stop the loading process | // an error here should stop the loading process | ||||
#define GET_TERRAIN_PROPERTY(val, prop, out)\ | #define GET_TERRAIN_PROPERTY(val, prop, out)\ | ||||
if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | ||||
{ LOGERROR("CMapReader::ParseTerrain() failed to get '%s' property", #prop);\ | { LOGERROR("CMapReader::ParseTerrain() failed to get '%s' property", #prop);\ | ||||
throw PSERROR_Game_World_MapLoadFailed("Error parsing terrain data.\nCheck application log for details"); } | throw PSERROR_Game_World_MapLoadFailed("Error parsing terrain data.\nCheck application log for details"); } | ||||
Show All 17 Lines | while (cur_terrain_tex < num_terrain_tex) | ||||
m_TerrainTextures.push_back(texentry); | m_TerrainTextures.push_back(texentry); | ||||
cur_terrain_tex++; | cur_terrain_tex++; | ||||
} | } | ||||
// build tile data | // build tile data | ||||
m_Tiles.resize(SQR(size)); | m_Tiles.resize(SQR(size)); | ||||
JS::RootedValue tileData(cx); | JS::RootedValue tileData(rq.cx); | ||||
GET_TERRAIN_PROPERTY(m_MapData, tileData, &tileData) | GET_TERRAIN_PROPERTY(m_MapData, tileData, &tileData) | ||||
// parse tile data object into flat arrays | // parse tile data object into flat arrays | ||||
std::vector<u16> tileIndex; | std::vector<u16> tileIndex; | ||||
std::vector<u16> tilePriority; | std::vector<u16> tilePriority; | ||||
GET_TERRAIN_PROPERTY(tileData, index, tileIndex); | GET_TERRAIN_PROPERTY(tileData, index, tileIndex); | ||||
GET_TERRAIN_PROPERTY(tileData, priority, tilePriority); | GET_TERRAIN_PROPERTY(tileData, priority, tilePriority); | ||||
Show All 24 Lines | |||||
#undef GET_TERRAIN_PROPERTY | #undef GET_TERRAIN_PROPERTY | ||||
return 0; | return 0; | ||||
} | } | ||||
int CMapReader::ParseEntities() | int CMapReader::ParseEntities() | ||||
{ | { | ||||
TIMER(L"ParseEntities"); | TIMER(L"ParseEntities"); | ||||
JSContext* cx = pSimulation2->GetScriptInterface().GetContext(); | ScriptInterface::Request rq(pSimulation2->GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
// parse entities from map data | // parse entities from map data | ||||
std::vector<Entity> entities; | std::vector<Entity> entities; | ||||
if (!pSimulation2->GetScriptInterface().GetProperty(m_MapData, "entities", entities)) | if (!pSimulation2->GetScriptInterface().GetProperty(m_MapData, "entities", entities)) | ||||
LOGWARNING("CMapReader::ParseEntities() failed to get 'entities' property"); | LOGWARNING("CMapReader::ParseEntities() failed to get 'entities' property"); | ||||
CSimulation2& sim = *pSimulation2; | CSimulation2& sim = *pSimulation2; | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | int CMapReader::ParseEntities() | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
int CMapReader::ParseEnvironment() | int CMapReader::ParseEnvironment() | ||||
{ | { | ||||
// parse environment settings from map data | // parse environment settings from map data | ||||
JSContext* cx = pSimulation2->GetScriptInterface().GetContext(); | ScriptInterface::Request rq(pSimulation2->GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
#define GET_ENVIRONMENT_PROPERTY(val, prop, out)\ | #define GET_ENVIRONMENT_PROPERTY(val, prop, out)\ | ||||
if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | ||||
LOGWARNING("CMapReader::ParseEnvironment() failed to get '%s' property", #prop); | LOGWARNING("CMapReader::ParseEnvironment() failed to get '%s' property", #prop); | ||||
JS::RootedValue envObj(cx); | JS::RootedValue envObj(rq.cx); | ||||
GET_ENVIRONMENT_PROPERTY(m_MapData, Environment, &envObj) | GET_ENVIRONMENT_PROPERTY(m_MapData, Environment, &envObj) | ||||
if (envObj.isUndefined()) | if (envObj.isUndefined()) | ||||
{ | { | ||||
LOGWARNING("CMapReader::ParseEnvironment(): Environment settings not found"); | LOGWARNING("CMapReader::ParseEnvironment(): Environment settings not found"); | ||||
return 0; | return 0; | ||||
} | } | ||||
Show All 16 Lines | #define GET_ENVIRONMENT_PROPERTY(val, prop, out)\ | ||||
GET_ENVIRONMENT_PROPERTY(envObj, TerrainAmbientColor, terrainAmbientColor) | GET_ENVIRONMENT_PROPERTY(envObj, TerrainAmbientColor, terrainAmbientColor) | ||||
m_LightEnv.m_TerrainAmbientColor = RGBColor(terrainAmbientColor.r, terrainAmbientColor.g, terrainAmbientColor.b); | m_LightEnv.m_TerrainAmbientColor = RGBColor(terrainAmbientColor.r, terrainAmbientColor.g, terrainAmbientColor.b); | ||||
CColor unitsAmbientColor; | CColor unitsAmbientColor; | ||||
GET_ENVIRONMENT_PROPERTY(envObj, UnitsAmbientColor, unitsAmbientColor) | GET_ENVIRONMENT_PROPERTY(envObj, UnitsAmbientColor, unitsAmbientColor) | ||||
m_LightEnv.m_UnitsAmbientColor = RGBColor(unitsAmbientColor.r, unitsAmbientColor.g, unitsAmbientColor.b); | m_LightEnv.m_UnitsAmbientColor = RGBColor(unitsAmbientColor.r, unitsAmbientColor.g, unitsAmbientColor.b); | ||||
// Water properties | // Water properties | ||||
JS::RootedValue waterObj(cx); | JS::RootedValue waterObj(rq.cx); | ||||
GET_ENVIRONMENT_PROPERTY(envObj, Water, &waterObj) | GET_ENVIRONMENT_PROPERTY(envObj, Water, &waterObj) | ||||
JS::RootedValue waterBodyObj(cx); | JS::RootedValue waterBodyObj(rq.cx); | ||||
GET_ENVIRONMENT_PROPERTY(waterObj, WaterBody, &waterBodyObj) | GET_ENVIRONMENT_PROPERTY(waterObj, WaterBody, &waterBodyObj) | ||||
// Water level - necessary | // Water level - necessary | ||||
float waterHeight; | float waterHeight; | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Height, waterHeight) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Height, waterHeight) | ||||
CmpPtr<ICmpWaterManager> cmpWaterManager(*pSimulation2, SYSTEM_ENTITY); | CmpPtr<ICmpWaterManager> cmpWaterManager(*pSimulation2, SYSTEM_ENTITY); | ||||
ENSURE(cmpWaterManager); | ENSURE(cmpWaterManager); | ||||
cmpWaterManager->SetWaterLevel(entity_pos_t::FromFloat(waterHeight)); | cmpWaterManager->SetWaterLevel(entity_pos_t::FromFloat(waterHeight)); | ||||
// If we have graphics, get rest of settings | // If we have graphics, get rest of settings | ||||
if (pWaterMan) | if (pWaterMan) | ||||
{ | { | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Type, pWaterMan->m_WaterType) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Type, pWaterMan->m_WaterType) | ||||
if (pWaterMan->m_WaterType == L"default") | if (pWaterMan->m_WaterType == L"default") | ||||
pWaterMan->m_WaterType = L"ocean"; | pWaterMan->m_WaterType = L"ocean"; | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Color, pWaterMan->m_WaterColor) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Color, pWaterMan->m_WaterColor) | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Tint, pWaterMan->m_WaterTint) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Tint, pWaterMan->m_WaterTint) | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Waviness, pWaterMan->m_Waviness) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Waviness, pWaterMan->m_Waviness) | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, Murkiness, pWaterMan->m_Murkiness) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, Murkiness, pWaterMan->m_Murkiness) | ||||
GET_ENVIRONMENT_PROPERTY(waterBodyObj, WindAngle, pWaterMan->m_WindAngle) | GET_ENVIRONMENT_PROPERTY(waterBodyObj, WindAngle, pWaterMan->m_WindAngle) | ||||
} | } | ||||
JS::RootedValue fogObject(cx); | JS::RootedValue fogObject(rq.cx); | ||||
GET_ENVIRONMENT_PROPERTY(envObj, Fog, &fogObject); | GET_ENVIRONMENT_PROPERTY(envObj, Fog, &fogObject); | ||||
GET_ENVIRONMENT_PROPERTY(fogObject, FogFactor, m_LightEnv.m_FogFactor); | GET_ENVIRONMENT_PROPERTY(fogObject, FogFactor, m_LightEnv.m_FogFactor); | ||||
GET_ENVIRONMENT_PROPERTY(fogObject, FogThickness, m_LightEnv.m_FogMax); | GET_ENVIRONMENT_PROPERTY(fogObject, FogThickness, m_LightEnv.m_FogMax); | ||||
CColor fogColor; | CColor fogColor; | ||||
GET_ENVIRONMENT_PROPERTY(fogObject, FogColor, fogColor); | GET_ENVIRONMENT_PROPERTY(fogObject, FogColor, fogColor); | ||||
m_LightEnv.m_FogColor = RGBColor(fogColor.r, fogColor.g, fogColor.b); | m_LightEnv.m_FogColor = RGBColor(fogColor.r, fogColor.g, fogColor.b); | ||||
JS::RootedValue postprocObject(cx); | JS::RootedValue postprocObject(rq.cx); | ||||
GET_ENVIRONMENT_PROPERTY(envObj, Postproc, &postprocObject); | GET_ENVIRONMENT_PROPERTY(envObj, Postproc, &postprocObject); | ||||
std::wstring postProcEffect; | std::wstring postProcEffect; | ||||
GET_ENVIRONMENT_PROPERTY(postprocObject, PostprocEffect, postProcEffect); | GET_ENVIRONMENT_PROPERTY(postprocObject, PostprocEffect, postProcEffect); | ||||
if (pPostproc) | if (pPostproc) | ||||
pPostproc->SetPostEffect(postProcEffect); | pPostproc->SetPostEffect(postProcEffect); | ||||
GET_ENVIRONMENT_PROPERTY(postprocObject, Brightness, m_LightEnv.m_Brightness); | GET_ENVIRONMENT_PROPERTY(postprocObject, Brightness, m_LightEnv.m_Brightness); | ||||
GET_ENVIRONMENT_PROPERTY(postprocObject, Contrast, m_LightEnv.m_Contrast); | GET_ENVIRONMENT_PROPERTY(postprocObject, Contrast, m_LightEnv.m_Contrast); | ||||
GET_ENVIRONMENT_PROPERTY(postprocObject, Saturation, m_LightEnv.m_Saturation); | GET_ENVIRONMENT_PROPERTY(postprocObject, Saturation, m_LightEnv.m_Saturation); | ||||
GET_ENVIRONMENT_PROPERTY(postprocObject, Bloom, m_LightEnv.m_Bloom); | GET_ENVIRONMENT_PROPERTY(postprocObject, Bloom, m_LightEnv.m_Bloom); | ||||
m_LightEnv.CalculateSunDirection(); | m_LightEnv.CalculateSunDirection(); | ||||
#undef GET_ENVIRONMENT_PROPERTY | #undef GET_ENVIRONMENT_PROPERTY | ||||
return 0; | return 0; | ||||
} | } | ||||
int CMapReader::ParseCamera() | int CMapReader::ParseCamera() | ||||
{ | { | ||||
JSContext* cx = pSimulation2->GetScriptInterface().GetContext(); | ScriptInterface::Request rq(pSimulation2->GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
// parse camera settings from map data | // parse camera settings from map data | ||||
// defaults if we don't find player starting camera | // defaults if we don't find player starting camera | ||||
float declination = DEGTORAD(30.f), rotation = DEGTORAD(-45.f); | float declination = DEGTORAD(30.f), rotation = DEGTORAD(-45.f); | ||||
CVector3D translation = CVector3D(100, 150, -100); | CVector3D translation = CVector3D(100, 150, -100); | ||||
#define GET_CAMERA_PROPERTY(val, prop, out)\ | #define GET_CAMERA_PROPERTY(val, prop, out)\ | ||||
if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | if (!pSimulation2->GetScriptInterface().GetProperty(val, #prop, out))\ | ||||
LOGWARNING("CMapReader::ParseCamera() failed to get '%s' property", #prop); | LOGWARNING("CMapReader::ParseCamera() failed to get '%s' property", #prop); | ||||
JS::RootedValue cameraObj(cx); | JS::RootedValue cameraObj(rq.cx); | ||||
GET_CAMERA_PROPERTY(m_MapData, Camera, &cameraObj) | GET_CAMERA_PROPERTY(m_MapData, Camera, &cameraObj) | ||||
if (!cameraObj.isUndefined()) | if (!cameraObj.isUndefined()) | ||||
{ // If camera property exists, read values | { // If camera property exists, read values | ||||
CFixedVector3D pos; | CFixedVector3D pos; | ||||
GET_CAMERA_PROPERTY(cameraObj, Position, pos) | GET_CAMERA_PROPERTY(cameraObj, Position, pos) | ||||
translation = pos; | translation = pos; | ||||
Show All 22 Lines |
Wildfire Games · Phabricator