Skip to content

Commit

Permalink
AngelScript: added message queue bindings.
Browse files Browse the repository at this point in the history
New script function: `game.pushMessage(msgType, dictionary@)` allows you to do practically any state change within the game (load/unload terrain, actor, connect/disconnect multiplayer). The parameters are passed as dictionary, documentation was added to /docs/angelscript.

The demo script (open ingame console and say 'loadscript demo_script.as') now displays a button "Launch simple test terrain" when in menu, as small test.
  • Loading branch information
ohlidalp committed Mar 7, 2023
1 parent 21fbc64 commit 04ff8fc
Show file tree
Hide file tree
Showing 15 changed files with 468 additions and 14 deletions.
6 changes: 6 additions & 0 deletions doc/angelscript/Script2Game/GameScriptClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ class GameScriptClass
*/
void quitGame();

/**
* Pushes a message to internal message queue. Parameters are listed in `Script2Game::MsgType` comments.
* @return True if the message was pushed, false if it was rejected.
*/
bool pushMessage(MsgType type, dictionary@ dict);

/// @}

/// @name GUI
Expand Down
73 changes: 73 additions & 0 deletions doc/angelscript/Script2Game/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,79 @@ enum BlinkType {
BLINK_WARN,
}

/**
* Binding of RoR::ActorModifyRequest::Type; use with `MSG_SIM_MODIFY_ACTOR_REQUESTED`
*/
enum ActorModifyRequestType
{
ACTOR_MODIFY_REQUEST_INVALID,
ACTOR_MODIFY_REQUEST_RELOAD, //!< Full reload from filesystem, requested by user
ACTOR_MODIFY_REQUEST_RESET_ON_INIT_POS,
ACTOR_MODIFY_REQUEST_RESET_ON_SPOT,
ACTOR_MODIFY_REQUEST_SOFT_RESET,
ACTOR_MODIFY_REQUEST_RESTORE_SAVED, //!< Internal, DO NOT USE
ACTOR_MODIFY_REQUEST_WAKE_UP
};

/**
* Binding of RoR::MsgType; Global gameplay message loop.
*/
enum MsgType
{
MSG_INVALID,
// Application
MSG_APP_SHUTDOWN_REQUESTED, //!< Immediate application shutdown. No params.
MSG_APP_SCREENSHOT_REQUESTED, //!< Capture screenshot. No params.
MSG_APP_DISPLAY_FULLSCREEN_REQUESTED, //!< Switch to fullscreen. No params.
MSG_APP_DISPLAY_WINDOWED_REQUESTED, //!< Switch to windowed display. No params.
MSG_APP_MODCACHE_LOAD_REQUESTED, //!< Internal for game startup, DO NOT PUSH MANUALLY.
MSG_APP_MODCACHE_UPDATE_REQUESTED, //!< Rescan installed mods and update cache. No params.
MSG_APP_MODCACHE_PURGE_REQUESTED, //!< Request cleanup and full rebuild of mod cache.
// Networking
MSG_NET_CONNECT_REQUESTED, //!< Request connection to multiplayer server specified by cvars 'mp_server_host, mp_server_port, mp_server_password'. No params.
MSG_NET_CONNECT_STARTED, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_CONNECT_PROGRESS, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_CONNECT_SUCCESS, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_CONNECT_FAILURE, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_SERVER_KICK, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_DISCONNECT_REQUESTED, //!< Request disconnect from multiplayer. No params.
MSG_NET_USER_DISCONNECT, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_RECV_ERROR, //!< Networking notification, DO NOT PUSH MANUALLY.
MSG_NET_REFRESH_SERVERLIST_SUCCESS, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_NET_REFRESH_SERVERLIST_FAILURE, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_NET_REFRESH_REPOLIST_SUCCESS, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_NET_OPEN_RESOURCE_SUCCESS, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_NET_REFRESH_REPOLIST_FAILURE, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_NET_REFRESH_AI_PRESETS, //!< Request refresh of AI presets menu in top menubar. No params.
// Simulation
MSG_SIM_PAUSE_REQUESTED, //!< Pause game. No params.
MSG_SIM_UNPAUSE_REQUESTED, //!< Unpause game. No params.
MSG_SIM_LOAD_TERRN_REQUESTED, //!< Request loading terrain. Param 'filename' (string)
MSG_SIM_LOAD_SAVEGAME_REQUESTED, //!< Request loading saved game. Param 'filename' (string)
MSG_SIM_UNLOAD_TERRN_REQUESTED, //!< Request returning to main menu. No params.
MSG_SIM_SPAWN_ACTOR_REQUESTED, //!< Request spawning an actor. Params: 'filename' (string), 'config' (string), 'skin' (string), 'enter' (bool), 'position' (vector3), 'rotation' (quaternion), 'free_position' (bool)
MSG_SIM_MODIFY_ACTOR_REQUESTED, //!< Request change of actor. Params: 'type' (enum ActorModifyRequestType)
MSG_SIM_DELETE_ACTOR_REQUESTED, //!< Request actor removal. Params: 'instance_id' (int)
MSG_SIM_SEAT_PLAYER_REQUESTED, //!< Put player character in a vehicle. Params: 'instance_id' (int), use -1 to get out of vehicle.
MSG_SIM_TELEPORT_PLAYER_REQUESTED, //!< Teleport player character anywhere on terrain. Param 'position' (vector3)
MSG_SIM_HIDE_NET_ACTOR_REQUESTED, //!< Request hiding of networked actor; used internally by top menubar. Params: 'instance_id' (int)
MSG_SIM_UNHIDE_NET_ACTOR_REQUESTED, //!< Request revealing of hidden networked actor; used internally by top menubar. Params: 'instance_id' (int)
// GUI
MSG_GUI_OPEN_MENU_REQUESTED,
MSG_GUI_CLOSE_MENU_REQUESTED,
MSG_GUI_OPEN_SELECTOR_REQUESTED, //!< Use `game.showChooser()` instead.
MSG_GUI_CLOSE_SELECTOR_REQUESTED, //!< No params.
MSG_GUI_MP_CLIENTS_REFRESH, //!< No params.
MSG_GUI_SHOW_MESSAGE_BOX_REQUESTED, //!< Use `game.showMessageBox()` instead.
MSG_GUI_DOWNLOAD_PROGRESS, //!< Background task notification, DO NOT PUSH MANUALLY.
MSG_GUI_DOWNLOAD_FINISHED, //!< Background task notification, DO NOT PUSH MANUALLY.
// Editing
MSG_EDI_MODIFY_GROUNDMODEL_REQUESTED, //!< Used by Friction UI, DO NOT PUSH MANUALLY.
MSG_EDI_ENTER_TERRN_EDITOR_REQUESTED, //!< No params.
MSG_EDI_LEAVE_TERRN_EDITOR_REQUESTED, //!< No params.
MSG_EDI_RELOAD_BUNDLE_REQUESTED, //!< Used internally for full reload of terrain/actor from disk, DO NOT PUSH MANUALLY.
};

} // namespace Script2Game

/// @} //addtogroup Script2Game
Expand Down
31 changes: 21 additions & 10 deletions resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ void frameStep(float dt)
// Show some game context
if (g_app_state.getInt() == 1) // main menu
{
ImGui::Text("Game state: main menu");
ImGui::Text("Pro tip: Press '"
+ inputs.getEventCommandTrimmed(EV_COMMON_CONSOLE_TOGGLE)
+ "' to open console anytime.");

// Reset simulation data
@g_displayed_document = null;
g_displayed_doc_filename = "";
g_terrain_tobj_files.removeRange(0, g_terrain_tobj_files.length());
drawMainMenuPanel();
}
else if (g_app_state.getInt() == 2) // simulation
{
Expand Down Expand Up @@ -353,4 +345,23 @@ void drawDocumentWindow()

ImGui::End();
ImGui::PopID(); //"document view"
}
}

void drawMainMenuPanel()
{
ImGui::Text("Game state: main menu");
ImGui::Text("Pro tip: Press '"
+ inputs.getEventCommandTrimmed(EV_COMMON_CONSOLE_TOGGLE)
+ "' to open console anytime.");

// Test message queue
if (ImGui::Button("Launch simple test terrain"))
{
game.pushMessage(MSG_SIM_LOAD_TERRN_REQUESTED, {{'filename', 'simple2.terrn2'}});
}

// Reset simulation data
@g_displayed_document = null;
g_displayed_doc_filename = "";
g_terrain_tobj_files.removeRange(0, g_terrain_tobj_files.length());
}
61 changes: 60 additions & 1 deletion source/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright 2007-2012 Thomas Fischer
Copyright 2013-2020 Petr Ohlidal
For more information, see http://www.rigsofrods.org/
For more information see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
Expand Down Expand Up @@ -468,4 +468,63 @@ std::string ToLocalizedString(IoInputGrabMode e)
}
}

const char* MsgTypeToString(MsgType type)
{
switch (type)
{
case MSG_APP_SHUTDOWN_REQUESTED : return "MSG_APP_SHUTDOWN_REQUESTED";
case MSG_APP_SCREENSHOT_REQUESTED : return "MSG_APP_SCREENSHOT_REQUESTED";
case MSG_APP_DISPLAY_FULLSCREEN_REQUESTED : return "MSG_APP_DISPLAY_FULLSCREEN_REQUESTED";
case MSG_APP_DISPLAY_WINDOWED_REQUESTED : return "MSG_APP_DISPLAY_WINDOWED_REQUESTED";
case MSG_APP_MODCACHE_LOAD_REQUESTED : return "MSG_APP_MODCACHE_LOAD_REQUESTED";
case MSG_APP_MODCACHE_UPDATE_REQUESTED : return "MSG_APP_MODCACHE_UPDATE_REQUESTED";
case MSG_APP_MODCACHE_PURGE_REQUESTED : return "MSG_APP_MODCACHE_PURGE_REQUESTED";

case MSG_NET_CONNECT_REQUESTED : return "MSG_NET_CONNECT_REQUESTED";
case MSG_NET_CONNECT_STARTED : return "MSG_NET_CONNECT_STARTED";
case MSG_NET_CONNECT_PROGRESS : return "MSG_NET_CONNECT_PROGRESS";
case MSG_NET_CONNECT_SUCCESS : return "MSG_NET_CONNECT_SUCCESS";
case MSG_NET_CONNECT_FAILURE : return "MSG_NET_CONNECT_FAILURE";
case MSG_NET_SERVER_KICK : return "MSG_NET_SERVER_KICK";
case MSG_NET_DISCONNECT_REQUESTED : return "MSG_NET_DISCONNECT_REQUESTED";
case MSG_NET_USER_DISCONNECT : return "MSG_NET_USER_DISCONNECT";
case MSG_NET_RECV_ERROR : return "MSG_NET_RECV_ERROR";
case MSG_NET_REFRESH_SERVERLIST_SUCCESS : return "MSG_NET_REFRESH_SERVERLIST_SUCCESS";
case MSG_NET_REFRESH_SERVERLIST_FAILURE : return "MSG_NET_REFRESH_SERVERLIST_FAILURE";
case MSG_NET_REFRESH_REPOLIST_SUCCESS : return "MSG_NET_REFRESH_REPOLIST_SUCCESS";
case MSG_NET_OPEN_RESOURCE_SUCCESS : return "MSG_NET_OPEN_RESOURCE_SUCCESS";
case MSG_NET_REFRESH_REPOLIST_FAILURE : return "MSG_NET_REFRESH_REPOLIST_FAILURE";
case MSG_NET_REFRESH_AI_PRESETS : return "MSG_NET_REFRESH_AI_PRESETS";

case MSG_SIM_PAUSE_REQUESTED : return "MSG_SIM_PAUSE_REQUESTED";
case MSG_SIM_UNPAUSE_REQUESTED : return "MSG_SIM_UNPAUSE_REQUESTED";
case MSG_SIM_LOAD_TERRN_REQUESTED : return "MSG_SIM_LOAD_TERRN_REQUESTED";
case MSG_SIM_LOAD_SAVEGAME_REQUESTED : return "MSG_SIM_LOAD_SAVEGAME_REQUESTED";
case MSG_SIM_UNLOAD_TERRN_REQUESTED : return "MSG_SIM_UNLOAD_TERRN_REQUESTED";
case MSG_SIM_SPAWN_ACTOR_REQUESTED : return "MSG_SIM_SPAWN_ACTOR_REQUESTED";
case MSG_SIM_MODIFY_ACTOR_REQUESTED : return "MSG_SIM_MODIFY_ACTOR_REQUESTED";
case MSG_SIM_DELETE_ACTOR_REQUESTED : return "MSG_SIM_DELETE_ACTOR_REQUESTED";
case MSG_SIM_SEAT_PLAYER_REQUESTED : return "MSG_SIM_SEAT_PLAYER_REQUESTED";
case MSG_SIM_TELEPORT_PLAYER_REQUESTED : return "MSG_SIM_TELEPORT_PLAYER_REQUESTED";
case MSG_SIM_HIDE_NET_ACTOR_REQUESTED : return "MSG_SIM_HIDE_NET_ACTOR_REQUESTED";
case MSG_SIM_UNHIDE_NET_ACTOR_REQUESTED : return "MSG_SIM_UNHIDE_NET_ACTOR_REQUESTED";

case MSG_GUI_OPEN_MENU_REQUESTED : return "MSG_GUI_OPEN_MENU_REQUESTED";
case MSG_GUI_CLOSE_MENU_REQUESTED : return "MSG_GUI_CLOSE_MENU_REQUESTED";
case MSG_GUI_OPEN_SELECTOR_REQUESTED : return "MSG_GUI_OPEN_SELECTOR_REQUESTED";
case MSG_GUI_CLOSE_SELECTOR_REQUESTED : return "MSG_GUI_CLOSE_SELECTOR_REQUESTED";
case MSG_GUI_MP_CLIENTS_REFRESH : return "MSG_GUI_MP_CLIENTS_REFRESH";
case MSG_GUI_SHOW_MESSAGE_BOX_REQUESTED : return "MSG_GUI_SHOW_MESSAGE_BOX_REQUESTED";
case MSG_GUI_DOWNLOAD_PROGRESS : return "MSG_GUI_DOWNLOAD_PROGRESS";
case MSG_GUI_DOWNLOAD_FINISHED : return "MSG_GUI_DOWNLOAD_FINISHED";

case MSG_EDI_MODIFY_GROUNDMODEL_REQUESTED : return "MSG_EDI_MODIFY_GROUNDMODEL_REQUESTED";
case MSG_EDI_ENTER_TERRN_EDITOR_REQUESTED : return "MSG_EDI_ENTER_TERRN_EDITOR_REQUESTED";
case MSG_EDI_LEAVE_TERRN_EDITOR_REQUESTED : return "MSG_EDI_LEAVE_TERRN_EDITOR_REQUESTED";
case MSG_EDI_RELOAD_BUNDLE_REQUESTED : return "MSG_EDI_RELOAD_BUNDLE_REQUESTED";

default: return "";
}
}

} // namespace RoR
6 changes: 4 additions & 2 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ enum MsgType
MSG_NET_RECV_ERROR,
MSG_NET_REFRESH_SERVERLIST_SUCCESS, //!< Payload = GUI::MpServerInfoVec* (owner)
MSG_NET_REFRESH_SERVERLIST_FAILURE,
MSG_NET_REFRESH_REPOLIST_SUCCESS, //!< Payload = GUI::ResourcesCollection* (owner)
MSG_NET_OPEN_RESOURCE_SUCCESS, //!< Payload = GUI::ResourcesCollection* (owner)
MSG_NET_REFRESH_REPOLIST_SUCCESS, //!< Payload = GUI::ResourcesCollection* (owner)
MSG_NET_OPEN_RESOURCE_SUCCESS, //!< Payload = GUI::ResourcesCollection* (owner)
MSG_NET_REFRESH_REPOLIST_FAILURE,
MSG_NET_REFRESH_AI_PRESETS,
// Simulation
Expand Down Expand Up @@ -117,6 +117,8 @@ enum MsgType
MSG_EDI_RELOAD_BUNDLE_REQUESTED, //!< Payload = RoR::CacheEntry* (weak)
};

const char* MsgTypeToString(MsgType type);

/// @} // addtogroup MsgQueue

enum class AppState
Expand Down
1 change: 1 addition & 0 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ if (USE_ANGELSCRIPT)
scripting/bindings/ImGuiAngelscript.cpp
scripting/bindings/InputEngineAngelscript.cpp
scripting/bindings/LocalStorageAngelscript.cpp
scripting/bindings/MsgQueueAngelscript.cpp
scripting/bindings/OgreAngelscript.cpp
scripting/bindings/ProceduralRoadAngelscript.cpp
scripting/bindings/ScriptEventsAngelscript.cpp
Expand Down
3 changes: 2 additions & 1 deletion source/main/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ ActorPtr GameContext::SpawnActor(ActorSpawnRequest& rq)
else
{
if (fresh_actor->ar_driveable != NOT_DRIVEABLE &&
rq.asr_origin != ActorSpawnRequest::Origin::NETWORK)
rq.asr_origin != ActorSpawnRequest::Origin::NETWORK &&
rq.asr_enter)
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(fresh_actor))));
}
Expand Down
1 change: 1 addition & 0 deletions source/main/physics/SimData.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ struct ActorSpawnRequest
int net_source_id = 0;
int net_stream_id = 0;
bool asr_free_position = false; //!< Disables the automatic spawn position adjustment
bool asr_enter = true;
bool asr_terrn_machine = false; //!< This is a fixed machinery
std::shared_ptr<rapidjson::Document>
asr_saved_state; //!< Pushes msg MODIFY_ACTOR (type RESTORE_SAVED) after spawn.
Expand Down

0 comments on commit 04ff8fc

Please sign in to comment.