RigsOfRods / rigs-of-rods

Main development repository for Rigs of Rods soft-body physics simulator
https://www.rigsofrods.org
GNU General Public License v3.0
995 stars 175 forks source link

Scripting: added `TerrainClass` #2971

Closed ohlidalp closed 1 year ago

ohlidalp commented 1 year ago

At the moment, it doesn't do anything useful, but it will become a base for terrain editing capabilities very soon. I decided to create a separate PR for it, to promote it and enable early testing.

To test it, open in-game console (using tilde hotkey or from top menubar) and copy/paste any of these lines:

as game.log(game.getTerrain().getHandle().getTerrainName());

as game.log(game.getTerrain().getHandle().getGUID());

as game.log(""+game.getTerrain().getHandle().getVersion());

as game.log(""+game.getTerrain().getHandle().isFlat());

as game.log("x:"+game.getTerrain().getHandle().getSpawnPos().x+" y:"+game.getTerrain().getHandle().getSpawnPos().y+" z:"+game.getTerrain().getHandle().getSpawnPos().z);

as 
  game.log("Name: "+game.getTerrain().getHandle().getTerrainName());
  game.log("GUID: "+game.getTerrain().getHandle().getGUID());
  game.log("Version: "+game.getTerrain().getHandle().getVersion());
  game.log("Is flat: "+game.getTerrain().getHandle().isFlat());
  game.log("Spawn pos: x="+game.getTerrain().getHandle().getSpawnPos().x+" y="+game.getTerrain().getHandle().getSpawnPos().y+" z="+game.getTerrain().getHandle().getSpawnPos().z);

image

Documentation will appear at https://developer.rigsofrods.org/d4/d07/group___script2_game.html after this PR is merged.

I realize we already have a lot of terrain-related functions in the GameScriptClass (global object game), see https://developer.rigsofrods.org/dc/d63/class_script2_game_1_1_game_script_class.html, but under the hood those are serviced by RoR::Terrain anyway, see https://developer.rigsofrods.org/d4/d4d/group___terrain.html, and I want to keep things organized.

Please mind the additional getHandle() function in the code snippets - that's a necessary evil to manage AngelScript reference counting simply and safely using https://github.com/only-a-ptr/RefCountingObject-AngelScript. Without it, adding new script feats is either laborous and bug-prone, or unsafe.

tritonas00 commented 1 year ago
In destructor ‘RefCountingObject<T>::~RefCountingObject() [with T = RoR::Terrain]’,
    inlined from ‘void RefCountingObject<T>::Release() [with T = RoR::Terrain]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:46:13,
    inlined from ‘void RefCountingObject<T>::Release() [with T = RoR::Terrain]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:40:10,
    inlined from ‘void RefCountingObjectPtr<T>::ReleaseHandle() [with T = RoR::Terrain]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:217:23,
    inlined from ‘void RefCountingObjectPtr<T>::Set(T*) [with T = RoR::Terrain]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:247:18,
    inlined from ‘RefCountingObjectPtr<T>& RefCountingObjectPtr<T>::operator=(const RefCountingObjectPtr<T>&) [with T = RoR::Terrain]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:237:8,
    inlined from ‘bool RoR::GameContext::LoadTerrain(const std::string&)’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/GameContext.cpp:141:71:
/home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:32:5: warning: ‘void operator delete(void*)’ called on pointer ‘((RefCountingObjectPtr<RoR::Terrain>*)this)[9].RefCountingObjectPtr<RoR::Terrain>::m_ref’ with nonzero offset 8 [-Wfree-nonheap-object]
   32 |     }

warning remains it seems

Copy pasting doesn't work here so i will leave that for others to test. So far no crashes though :+1:

CuriousMike56 commented 1 year ago

Commands work fine here: image