Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting: added TerrainClass #2971

Merged
merged 2 commits into from
Nov 23, 2022
Merged

Conversation

ohlidalp
Copy link
Member

@ohlidalp ohlidalp commented Nov 23, 2022

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.

This introduces `RefCountingObject` to the codebase. Using it makes it possible to pass object between C++ and AngelScript without worry. See /~https://github.com/only-a-ptr/RefCountingObject-AngelScript

Signifficant codechanges:
 - global variable `App::g_sim_terrain` removed, replaced by new variable `GameContext::m_terrain`.
 - global function `App::GetSimTerrain()` removed, using `GameContext::GetTerrain()`
 - static factory func `Terrain* Terrain::LoadAndPrepareTerrain()` changed to nonstatic `void Terrain::initialize()`, loading now done by GameContext.
 - Terrain.h: added function `dispose()` and flag `bool m_disposed` - after the terrain is unloaded, the scripts may still hold pointers to it. Therefore it must remain in memory, even though all the component objects were deleted.
@tritonas00
Copy link
Collaborator

tritonas00 commented Nov 23, 2022

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 👍

@CuriousMike56
Copy link
Member

Commands work fine here:
image

@ohlidalp ohlidalp merged commit 2f5dc1c into RigsOfRods:master Nov 23, 2022
@ohlidalp ohlidalp deleted the script_terrain branch November 23, 2022 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants