Skip to content

Commit

Permalink
AngelScript: added TerrainClass (ref)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removed `int GameScript::getLoadedTerrain(string&)`.

INTERNAL:
* global variable `App::g_sim_terrain` removed, replaced by new variable `GameContext::m_terrain`.
* static factory func Terrain::LoadAndPrepareTerrain() changed to Terrain::initialize(), loading now done by GameContext.
  • Loading branch information
ohlidalp committed Aug 7, 2022
1 parent fb5250f commit b9eb90d
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 113 deletions.
4 changes: 1 addition & 3 deletions doc/angelscript/Script2Game/GameScriptClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ class GameScriptClass

/**
* Gets the currently loaded terrain
* @param result This string will contain the name of the terrain after you called this function
* @return 0 on success
*/
int getLoadedTerrain(string result);
TerrainClass getTerrain();

/**
* Returns the current position of the person
Expand Down
48 changes: 48 additions & 0 deletions doc/angelscript/Script2Game/TerrainClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

namespace Script2Game {

/** \addtogroup ScriptSideAPIs
* @{
*/

/** \addtogroup Script2Game
* @{
*/

/**
* @brief Binding of RoR::Terrain; represents a loaded terrain.
* @note Obtain the object using `game.getTerrain()`.
*/
class TerrainClass
{
public:
/**
* @return Full name of the terrain
*/
string getTerrainName();

/**
* @return GUID (global unique ID) of the terrain, or empty string if not specified.
*/
string getGUID();

/**
* @return true if specified as flat (no heightmap).
*/
bool isFlat();

/**
* @return version of the terrain, as specified by author.
*/
int getVersion();

/**
* @return Player spawn position when entering game.
*/
vector3 getSpawnPos();
};

/// @} //addtogroup Script2Game
/// @} //addtogroup ScriptSideAPIs

} //namespace Script2Game
5 changes: 2 additions & 3 deletions source/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "Network.h"
#include "ScriptEngine.h"
#include "SoundScriptManager.h"
#include "Terrain.h"
#include "ThreadPool.h"

namespace RoR {
Expand All @@ -60,7 +61,6 @@ static GUIManager* g_gui_manager;
static InputEngine* g_input_engine;
static CacheSystem* g_cache_system;
static MumbleIntegration* g_mumble;
static Terrain* g_sim_terrain;
static ThreadPool* g_thread_pool;
static CameraManager* g_camera_manager;
static GfxScene g_gfx_scene;
Expand Down Expand Up @@ -238,7 +238,6 @@ CVar* gfx_reduce_shadows;
CVar* gfx_enable_rtshaders;

// Instance management
void SetSimTerrain (Terrain* obj) { g_sim_terrain = obj;}
void SetCacheSystem (CacheSystem* obj) { g_cache_system = obj; }

// Instance access
Expand All @@ -250,7 +249,7 @@ Console* GetConsole () { return &g_console;}
InputEngine* GetInputEngine () { return g_input_engine;}
CacheSystem* GetCacheSystem () { return g_cache_system;}
MumbleIntegration* GetMumble () { return g_mumble; }
Terrain* GetSimTerrain () { return g_sim_terrain; }
Terrain* GetSimTerrain () { return g_game_context.GetTerrain().get(); }
ThreadPool* GetThreadPool () { return g_thread_pool; }
CameraManager* GetCameraManager () { return g_camera_manager; }
GfxScene* GetGfxScene () { return &g_gfx_scene; }
Expand Down
3 changes: 1 addition & 2 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ Console* GetConsole();
InputEngine* GetInputEngine();
CacheSystem* GetCacheSystem();
MumbleIntegration* GetMumble();
Terrain* GetSimTerrain();
Terrain* GetSimTerrain();
ThreadPool* GetThreadPool();
CameraManager* GetCameraManager();
GfxScene* GetGfxScene();
Expand All @@ -466,7 +466,6 @@ void CreateSoundScriptManager();
void CreateScriptEngine();

// Setters
void SetSimTerrain (Terrain* obj);
void SetCacheSystem (CacheSystem* obj);

// Cleanups
Expand Down
1 change: 1 addition & 0 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ if (USE_ANGELSCRIPT)
scripting/bindings/LocalStorageAngelscript.cpp
scripting/bindings/OgreAngelscript.cpp
scripting/bindings/ScriptEventsAngelscript.cpp
scripting/bindings/TerrainAngelscript.cpp
scripting/bindings/VehicleAiAngelscript.cpp
)
endif ()
Expand Down
96 changes: 61 additions & 35 deletions source/main/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,41 @@ bool GameContext::LoadTerrain(std::string const& filename_part)
// Init resources
App::GetCacheSystem()->LoadResource(*terrn_entry);

// Perform the loading and setup
App::SetSimTerrain(RoR::Terrain::LoadAndPrepareTerrain(terrn_entry));
if (!App::GetSimTerrain())
// Load the terrain
Terrn2Def terrn2;
std::string const& filename = terrn_entry->fname;
try
{
Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(filename);
LOG(" ===== LOADING TERRAIN " + filename);
Terrn2Parser parser;
if (! parser.LoadTerrn2(terrn2, stream))
{
return false; // Errors already logged to console
}
}
catch (Ogre::Exception& e)
{
App::GetGuiManager()->ShowMessageBox(_L("Terrain loading error"), e.getFullDescription().c_str());
return false;
}

// CAUTION - the global instance must be set during init! Needed by:
// * GameScript::spawnObject()
// * ProceduralManager
// * Landusemap
// * SurveyMapTextureCreator
// * Collisions (debug visualization)
m_terrain = new RoR::Terrain(terrn_entry, terrn2);
if (!m_terrain->initialize())
{
m_terrain = nullptr; // TerrainPtr deletes the object
return false; // Message box already displayed
}

// Initialize envmap textures by rendering center of map
Ogre::Vector3 center = App::GetSimTerrain()->getMaxTerrainSize() / 2;
center.y = App::GetSimTerrain()->GetHeightAt(center.x, center.z) + 1.0f;
Ogre::Vector3 center = m_terrain->getMaxTerrainSize() / 2;
center.y = m_terrain->GetHeightAt(center.x, center.z) + 1.0f;
App::GetGfxScene()->GetEnvMap().UpdateEnvMap(center, /*gfx_actor:*/nullptr, /*full:*/true);

// Scan groundmodels
Expand All @@ -131,11 +156,12 @@ bool GameContext::LoadTerrain(std::string const& filename_part)

void GameContext::UnloadTerrain()
{
if (App::GetSimTerrain() != nullptr)
if (m_terrain != nullptr)
{
// remove old terrain
delete(App::GetSimTerrain());
App::SetSimTerrain(nullptr);
// dispose(), do not `delete` - script may still hold reference to the object.
m_terrain->dispose();
// release local reference - object will be deleted when all references are released.
m_terrain = nullptr;
}
}

Expand All @@ -157,7 +183,7 @@ Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
float h = m_player_actor->getMaxHeight(true);
rq.asr_rotation = Ogre::Quaternion(Ogre::Degree(270) - Ogre::Radian(m_player_actor->getRotation()), Ogre::Vector3::UNIT_Y);
rq.asr_position = m_player_actor->getRotationCenter();
rq.asr_position.y = App::GetSimTerrain()->GetCollisions()->getSurfaceHeightBelow(rq.asr_position.x, rq.asr_position.z, h);
rq.asr_position.y = m_terrain->GetCollisions()->getSurfaceHeightBelow(rq.asr_position.x, rq.asr_position.z, h);
rq.asr_position.y += m_player_actor->getHeightAboveGroundBelow(h, true); // retain height above ground
}
else
Expand Down Expand Up @@ -421,11 +447,11 @@ void GameContext::ChangePlayerActor(Actor* actor)
// actor has a cinecam (find optimal exit position)
Ogre::Vector3 l = position - 2.0f * prev_player_actor->GetCameraRoll();
Ogre::Vector3 r = position + 2.0f * prev_player_actor->GetCameraRoll();
float l_h = App::GetSimTerrain()->GetCollisions()->getSurfaceHeightBelow(l.x, l.z, l.y + h);
float r_h = App::GetSimTerrain()->GetCollisions()->getSurfaceHeightBelow(r.x, r.z, r.y + h);
float l_h = m_terrain->GetCollisions()->getSurfaceHeightBelow(l.x, l.z, l.y + h);
float r_h = m_terrain->GetCollisions()->getSurfaceHeightBelow(r.x, r.z, r.y + h);
position = std::abs(r.y - r_h) * 1.2f < std::abs(l.y - l_h) ? r : l;
}
position.y = App::GetSimTerrain()->GetCollisions()->getSurfaceHeightBelow(position.x, position.z, position.y + h);
position.y = m_terrain->GetCollisions()->getSurfaceHeightBelow(position.x, position.z, position.y + h);

Character* player_character = this->GetPlayerCharacter();
if (player_character)
Expand Down Expand Up @@ -491,7 +517,7 @@ void GameContext::UpdateActors()

Actor* GameContext::FindActorByCollisionBox(std::string const & ev_src_instance_name, std::string const & box_name)
{
return m_actor_manager.FindActorInsideBox(App::GetSimTerrain()->GetCollisions(),
return m_actor_manager.FindActorInsideBox(m_terrain->GetCollisions(),
ev_src_instance_name, box_name);
}

Expand Down Expand Up @@ -549,12 +575,12 @@ void GameContext::ShowLoaderGUI(int type, const Ogre::String& instance, const Og
// first, test if the place if clear, BUT NOT IN MULTIPLAYER
if (!(App::mp_state->getEnum<MpState>() == MpState::CONNECTED))
{
collision_box_t* spawnbox = App::GetSimTerrain()->GetCollisions()->getBox(instance, box);
collision_box_t* spawnbox = m_terrain->GetCollisions()->getBox(instance, box);
for (auto actor : this->GetActorManager()->GetActors())
{
for (int i = 0; i < actor->ar_num_nodes; i++)
{
if (App::GetSimTerrain()->GetCollisions()->isInside(actor->ar_nodes[i].AbsPosition, spawnbox))
if (m_terrain->GetCollisions()->isInside(actor->ar_nodes[i].AbsPosition, spawnbox))
{
App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_NOTICE, _L("Please clear the place first"), "error.png");
return;
Expand All @@ -563,9 +589,9 @@ void GameContext::ShowLoaderGUI(int type, const Ogre::String& instance, const Og
}
}

m_current_selection.asr_position = App::GetSimTerrain()->GetCollisions()->getPosition(instance, box);
m_current_selection.asr_rotation = App::GetSimTerrain()->GetCollisions()->getDirection(instance, box);
m_current_selection.asr_spawnbox = App::GetSimTerrain()->GetCollisions()->getBox(instance, box);
m_current_selection.asr_position = m_terrain->GetCollisions()->getPosition(instance, box);
m_current_selection.asr_rotation = m_terrain->GetCollisions()->getDirection(instance, box);
m_current_selection.asr_spawnbox = m_terrain->GetCollisions()->getBox(instance, box);

RoR::Message m(MSG_GUI_OPEN_SELECTOR_REQUESTED);
m.payload = reinterpret_cast<void*>(new LoaderType(LoaderType(type)));
Expand Down Expand Up @@ -641,12 +667,12 @@ void GameContext::CreatePlayerCharacter()
m_character_factory.CreateLocalCharacter();

// Adjust character position
Ogre::Vector3 spawn_pos = App::GetSimTerrain()->getSpawnPos();
Ogre::Vector3 spawn_pos = m_terrain->getSpawnPos();
float spawn_rot = 0.0f;

// Classic behavior, retained for compatibility.
// Required for maps like N-Labs or F1 Track.
if (!App::GetSimTerrain()->HasPredefinedActors())
if (!m_terrain->HasPredefinedActors())
{
spawn_rot = 180.0f;
}
Expand Down Expand Up @@ -675,7 +701,7 @@ void GameContext::CreatePlayerCharacter()
App::diag_preset_spawn_rot->setStr("");
}

spawn_pos.y = App::GetSimTerrain()->GetCollisions()->getSurfaceHeightBelow(spawn_pos.x, spawn_pos.z, spawn_pos.y + 1.8f);
spawn_pos.y = m_terrain->GetCollisions()->getSurfaceHeightBelow(spawn_pos.x, spawn_pos.z, spawn_pos.y + 1.8f);

this->GetPlayerCharacter()->setPosition(spawn_pos);
this->GetPlayerCharacter()->setRotation(Ogre::Degree(spawn_rot));
Expand All @@ -699,7 +725,7 @@ Character* GameContext::GetPlayerCharacter() // Convenience ~ counterpart of `Ge

void GameContext::TeleportPlayer(float x, float z)
{
float y = App::GetSimTerrain()->GetCollisions()->getSurfaceHeight(x, z);
float y = m_terrain->GetCollisions()->getSurfaceHeight(x, z);
if (!this->GetPlayerActor())
{
this->GetPlayerCharacter()->setPosition(Ogre::Vector3(x, y, z));
Expand All @@ -720,9 +746,9 @@ void GameContext::TeleportPlayer(float x, float z)
for (int i = 0; i < actor->ar_num_nodes; i++)
{
Ogre::Vector3 pos = actor->ar_nodes[i].AbsPosition;
src_agl = std::min(pos.y - App::GetSimTerrain()->GetCollisions()->getSurfaceHeight(pos.x, pos.z), src_agl);
src_agl = std::min(pos.y - m_terrain->GetCollisions()->getSurfaceHeight(pos.x, pos.z), src_agl);
pos += translation;
dst_agl = std::min(pos.y - App::GetSimTerrain()->GetCollisions()->getSurfaceHeight(pos.x, pos.z), dst_agl);
dst_agl = std::min(pos.y - m_terrain->GetCollisions()->getSurfaceHeight(pos.x, pos.z), dst_agl);
}
}

Expand Down Expand Up @@ -966,7 +992,7 @@ void GameContext::UpdateSkyInputEvents(float dt)
{
#ifdef USE_CAELUM
if (App::gfx_sky_mode->getEnum<GfxSkyMode>() == GfxSkyMode::CAELUM &&
App::GetSimTerrain()->getSkyManager())
m_terrain->getSkyManager())
{
float time_factor = 1.0f;

Expand All @@ -991,37 +1017,37 @@ void GameContext::UpdateSkyInputEvents(float dt)
time_factor = App::gfx_sky_time_speed->getInt();
}

if (App::GetSimTerrain()->getSkyManager()->GetSkyTimeFactor() != time_factor)
if (m_terrain->getSkyManager()->GetSkyTimeFactor() != time_factor)
{
App::GetSimTerrain()->getSkyManager()->SetSkyTimeFactor(time_factor);
Str<200> msg; msg << _L("Time set to ") << App::GetSimTerrain()->getSkyManager()->GetPrettyTime();
m_terrain->getSkyManager()->SetSkyTimeFactor(time_factor);
Str<200> msg; msg << _L("Time set to ") << m_terrain->getSkyManager()->GetPrettyTime();
RoR::App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_NOTICE, msg.ToCStr());
}
}

#endif // USE_CAELUM
if (App::gfx_sky_mode->getEnum<GfxSkyMode>() == GfxSkyMode::SKYX &&
App::GetSimTerrain()->getSkyXManager())
m_terrain->getSkyXManager())
{
if (RoR::App::GetInputEngine()->getEventBoolValue(EV_SKY_INCREASE_TIME))
{
App::GetSimTerrain()->getSkyXManager()->GetSkyX()->setTimeMultiplier(1.0f);
m_terrain->getSkyXManager()->GetSkyX()->setTimeMultiplier(1.0f);
}
else if (RoR::App::GetInputEngine()->getEventBoolValue(EV_SKY_INCREASE_TIME_FAST))
{
App::GetSimTerrain()->getSkyXManager()->GetSkyX()->setTimeMultiplier(2.0f);
m_terrain->getSkyXManager()->GetSkyX()->setTimeMultiplier(2.0f);
}
else if (RoR::App::GetInputEngine()->getEventBoolValue(EV_SKY_DECREASE_TIME))
{
App::GetSimTerrain()->getSkyXManager()->GetSkyX()->setTimeMultiplier(-1.0f);
m_terrain->getSkyXManager()->GetSkyX()->setTimeMultiplier(-1.0f);
}
else if (RoR::App::GetInputEngine()->getEventBoolValue(EV_SKY_DECREASE_TIME_FAST))
{
App::GetSimTerrain()->getSkyXManager()->GetSkyX()->setTimeMultiplier(-2.0f);
m_terrain->getSkyXManager()->GetSkyX()->setTimeMultiplier(-2.0f);
}
else
{
App::GetSimTerrain()->getSkyXManager()->GetSkyX()->setTimeMultiplier(0.01f);
m_terrain->getSkyXManager()->GetSkyX()->setTimeMultiplier(0.01f);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions source/main/GameContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "RecoveryMode.h"
#include "SceneMouse.h"
#include "SimData.h"
#include "Terrain.h"

#include <list>
#include <mutex>
Expand Down Expand Up @@ -109,6 +110,7 @@ class GameContext

bool LoadTerrain(std::string const& filename_part);
void UnloadTerrain();
TerrainPtr& GetTerrain() { return m_terrain; }

/// @}
/// @name Actors
Expand Down Expand Up @@ -179,6 +181,9 @@ class GameContext
Message* m_msg_chain_end = nullptr;
std::mutex m_msg_mutex;

// Terrain
TerrainPtr m_terrain;

// Actors (physics and netcode)
ActorManager m_actor_manager;
Actor* m_player_actor = nullptr; //!< Actor (vehicle or machine) mounted and controlled by player
Expand Down
3 changes: 1 addition & 2 deletions source/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,7 @@ int main(int argc, char *argv[])
App::GetGuiManager()->MenuWallpaper->show();
App::sim_state->setVal((int)SimState::OFF);
App::app_state->setVal((int)AppState::MAIN_MENU);
delete App::GetSimTerrain();
App::SetSimTerrain(nullptr);
App::GetGameContext()->UnloadTerrain();
App::GetGfxScene()->ClearScene();
App::sim_terrain_name->setStr("");
App::sim_terrain_gui_name->setStr("");
Expand Down
Loading

0 comments on commit b9eb90d

Please sign in to comment.