Skip to content

Commit

Permalink
Started on working on ThriveServer executable and separating common
Browse files Browse the repository at this point in the history
code from ThriveGame to ThriveCommon
  • Loading branch information
hhyyrylainen committed Dec 3, 2018
1 parent ba3d425 commit 7f0f7ee
Show file tree
Hide file tree
Showing 12 changed files with 1,006 additions and 243 deletions.
2 changes: 1 addition & 1 deletion SetupThrive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def parseExtraArgs
leviathan = Leviathan.new(
# Use this if you always want the latest commit
# version: "develop",
version: "7ebab71614a51cc1cfddbd84a2cd1c8eef8fd294",
version: "e6c7f7f82be32fe48f92c11a21d91d4fde44da1a",
# Doesn't actually work, but leviathan doesn't install with sudo by
# default, or install at all for that matter
noInstallSudo: true
Expand Down
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,14 @@ set(GROUP_MENU
# For testing purposes even these quite heavily to the final
# executable connected files are included in the lib
set(GROUP_CORE_FILES
# TODO: to move these out of here the script building would need
# IF-DEFS everywhere for switching when ThriveGame isn't available
"ThriveGame.h" "ThriveGame.cpp"
"thrive_net_handler.h" "thrive_net_handler.cpp"
"thrive_common.h" "thrive_common.cpp"
# These might not be needed here
"server/ThriveServer.h" "server/ThriveServer.cpp"
"server/thrive_server_net_handler.h" "server/thrive_server_net_handler.cpp"
)


Expand Down Expand Up @@ -227,4 +233,7 @@ include(LeviathanUsingProject)
# The project is now defined
target_link_libraries(Thrive ThriveLib)

# Server target. This also defines a Leviathan program
add_subdirectory(server)


240 changes: 15 additions & 225 deletions src/ThriveGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "thrive_world_factory.h"


#include <Addons/GameModuleLoader.h>
#include <GUI/GuiView.h>
#include <Handlers/ObjectLoader.h>
#include <Networking/NetworkHandler.h>
Expand Down Expand Up @@ -130,14 +129,6 @@ class ThriveGame::Implementation {
std::shared_ptr<CellStageWorld> m_cellStage;
std::shared_ptr<MicrobeEditorWorld> m_microbeEditor;

// This contains all the microbe_stage AngelScript code
Leviathan::GameModule::pointer m_microbeScripts;

// This is "temporarily" merged with the microbe scripts as this needs to
// share some types
// // This contains all the microbe_editor AngelScript code
// Leviathan::GameModule::pointer m_MicrobeEditorScripts;

//! This is the background object of the cell stage
Ogre::MeshPtr m_microbeBackgroundMesh;
Ogre::SubMesh* m_microbeBackgroundSubMesh;
Expand Down Expand Up @@ -188,15 +179,15 @@ ThriveGame* ThriveGame::StaticGame = nullptr;
Leviathan::NetworkInterface*
ThriveGame::_GetApplicationPacketHandler()
{
if(!Network)
Network = std::make_unique<ThriveNetHandler>();
return Network.get();
if(!m_network)
m_network = std::make_unique<ThriveNetHandler>();
return m_network.get();
}

void
ThriveGame::_ShutdownApplicationPacketHandler()
{
Network.reset();
m_network.reset();
}
// ------------------------------------ //
void
Expand Down Expand Up @@ -276,7 +267,7 @@ void

// Setup compound clouds //

// This is needed for the compound clouds to work in generale
// This is needed for the compound clouds to work in general
const auto compoundCount = SimulationParameters::compoundRegistry.getSize();

LEVIATHAN_ASSERT(SimulationParameters::compoundRegistry.getSize() > 0,
Expand All @@ -303,14 +294,14 @@ void
// Let the script do setup //
// This registers all the script defined systems to run and be
// available from the world
LEVIATHAN_ASSERT(m_impl->m_microbeScripts, "microbe scripts not loaded");
LEVIATHAN_ASSERT(getMicrobeScripts(), "microbe scripts not loaded");

LOG_INFO("Calling world setup script setupScriptsForWorld");

ScriptRunningSetup setup;
setup.SetEntrypoint("setupScriptsForWorld");

auto result = m_impl->m_microbeScripts->ExecuteOnModule<void>(
auto result = getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, m_impl->m_cellStage.get());

if(result.Result != SCRIPT_RUN_RESULT::Success) {
Expand Down Expand Up @@ -376,7 +367,7 @@ void
// Spawn player //
setup = ScriptRunningSetup("setupPlayer");

result = m_impl->m_microbeScripts->ExecuteOnModule<void>(
result = getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, m_impl->m_cellStage.get());

if(result.Result != SCRIPT_RUN_RESULT::Success) {
Expand Down Expand Up @@ -407,44 +398,6 @@ void
//(the script will disable the tutorial etc)
}
// ------------------------------------ //
bool
ThriveGame::scriptSetup()
{
LOG_INFO("Calling global setup script setupProcesses");

ScriptRunningSetup setup("setupProcesses");

auto result = m_impl->m_microbeScripts->ExecuteOnModule<void>(setup, false);

if(result.Result != SCRIPT_RUN_RESULT::Success) {

LOG_ERROR(
"Failed to run script setup function: " + setup.Entryfunction);
return false;
}

LOG_INFO("Finished calling the above setup script");

LOG_INFO("Calling global setup script setupOrganelles");

setup = ScriptRunningSetup("setupOrganelles");

result = m_impl->m_microbeScripts->ExecuteOnModule<void>(setup, false);

if(result.Result != SCRIPT_RUN_RESULT::Success) {

LOG_ERROR(
"Failed to run script setup function: " + setup.Entryfunction);
return false;
}

LOG_INFO("Finished calling the above setup script");


LOG_INFO("Finished calling script setup");
return true;
}
// ------------------------------------ //
CellStageWorld*
ThriveGame::getCellStage()
{
Expand All @@ -462,12 +415,6 @@ PlayerMicrobeControl*
{
return m_impl->m_cellStageKeys.get();
}

Leviathan::GameModule*
ThriveGame::getMicrobeScripts()
{
return m_impl->m_microbeScripts.get();
}
// ------------------------------------ //
void
ThriveGame::killPlayerCellClicked()
Expand All @@ -476,7 +423,7 @@ void

ScriptRunningSetup setup = ScriptRunningSetup("killPlayerCellClicked");

auto result = m_impl->m_microbeScripts->ExecuteOnModule<void>(
auto result = getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, m_impl->m_cellStage.get());

if(result.Result != SCRIPT_RUN_RESULT::Success) {
Expand Down Expand Up @@ -574,7 +521,7 @@ void

ScriptRunningSetup setup("onEditorEntry");

auto result = m_impl->m_microbeScripts->ExecuteOnModule<void>(
auto result = getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, m_impl->m_microbeEditor.get());

if(result.Result != SCRIPT_RUN_RESULT::Success) {
Expand Down Expand Up @@ -625,14 +572,13 @@ void
// Let the script do setup //
// This registers all the script defined systems to run and be
// available from the world
LEVIATHAN_ASSERT(
m_impl->m_microbeScripts, "microbe stage scripts not loaded");
LEVIATHAN_ASSERT(getMicrobeScripts(), "microbe stage scripts not loaded");

LOG_INFO("Calling return from editor script, onReturnFromEditor");

ScriptRunningSetup setup("onReturnFromEditor");

auto result = m_impl->m_microbeScripts->ExecuteOnModule<void>(
auto result = getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, m_impl->m_cellStage.get());

if(result.Result != SCRIPT_RUN_RESULT::Success) {
Expand Down Expand Up @@ -711,29 +657,13 @@ void
return;
}

// Load json data //
SimulationParameters::init();

// Load scripts
LOG_INFO("ThriveGame: loading main scripts");
if(!loadScriptsAndConfigs()) {

// TODO: should these load failures be fatal errors (process would exit
// immediately)

try {
m_impl->m_microbeScripts =
engine->GetGameModuleLoader()->Load("microbe_stage", "ThriveGame");
} catch(const Leviathan::Exception& e) {

LOG_ERROR(
"ThriveGame: microbe_stage module failed to load, exception:");
e.PrintToLog();
LOG_ERROR("Failed to load init data, quitting");
MarkAsClosing();
return;
}

LOG_INFO("ThriveGame: script loading succeeded");

if(!scriptSetup()) {

LOG_ERROR("ThriveGame: failed to run setup script functions");
Expand Down Expand Up @@ -773,151 +703,11 @@ void
}
}
// ------------------------------------ //
//! \note This isn't actually called
void
cellHitAgent(Leviathan::PhysicalWorld& physicalWorld,
Leviathan::PhysicsBody& first,
Leviathan::PhysicsBody& second)
{
// GameWorld* gameWorld = physicalWorld.GetGameWorld();

// ScriptRunningSetup setup("cellHitAgent");

// auto result =
// ThriveGame::Get()->getMicrobeScripts()->ExecuteOnModule<void>(
// setup, false, gameWorld, first.GetOwningEntity(),
// second.GetOwningEntity());

// if(result.Result != SCRIPT_RUN_RESULT::Success)
// LOG_ERROR("Failed to run script side cellHitAgent");
}

void
cellHitFloatingOrganelle(Leviathan::PhysicalWorld& physicalWorld,
Leviathan::PhysicsBody& first,
Leviathan::PhysicsBody& second)
{
GameWorld* gameWorld = physicalWorld.GetGameWorld();

ScriptRunningSetup setup("cellHitFloatingOrganelle");

auto result = ThriveGame::Get()->getMicrobeScripts()->ExecuteOnModule<void>(
setup, false, gameWorld, first.GetOwningEntity(),
second.GetOwningEntity());

if(result.Result != SCRIPT_RUN_RESULT::Success)
LOG_ERROR("Failed to run script side cellHitFloatingOrganelle");
}

//! \todo This should return false when either cell is engulfing and apply the
//! damaging effect
bool
cellOnCellAABBHitCallback(Leviathan::PhysicalWorld& physicalWorld,
Leviathan::PhysicsBody& first,
Leviathan::PhysicsBody& second)
{
GameWorld* gameWorld = physicalWorld.GetGameWorld();

ScriptRunningSetup setup("beingEngulfed");

auto returned =
ThriveGame::Get()->getMicrobeScripts()->ExecuteOnModule<bool>(setup,
false, gameWorld, first.GetOwningEntity(),
second.GetOwningEntity());

if(returned.Result != SCRIPT_RUN_RESULT::Success) {
LOG_ERROR("Failed to run script side beingEngulfed");
return true;
}

return returned.Value;
}


bool
agentCallback(Leviathan::PhysicalWorld& physicalWorld,
Leviathan::PhysicsBody& first,
Leviathan::PhysicsBody& second)
{
GameWorld* gameWorld = physicalWorld.GetGameWorld();

// Now we can do more interetsing things with agents
ScriptRunningSetup setup("hitAgent");

auto returned =
ThriveGame::Get()->getMicrobeScripts()->ExecuteOnModule<bool>(setup,
false, gameWorld, first.GetOwningEntity(),
second.GetOwningEntity());

if(returned.Result != SCRIPT_RUN_RESULT::Success) {
LOG_ERROR("Failed to run script side hitAgent");
return true;
}

return returned.Value;
}


void
cellOnCellActualContact(Leviathan::PhysicalWorld& physicalWorld,
Leviathan::PhysicsBody& first,
Leviathan::PhysicsBody& second)
{
// This will call a script that pulls cells in towards engulfers
GameWorld* gameWorld = physicalWorld.GetGameWorld();

ScriptRunningSetup setup("cellOnCellActualContact");

auto returned =
ThriveGame::Get()->getMicrobeScripts()->ExecuteOnModule<void>(setup,
false, gameWorld, first.GetOwningEntity(),
second.GetOwningEntity());

if(returned.Result != SCRIPT_RUN_RESULT::Success) {
LOG_ERROR("Failed to run script side beingEngulfed");
}
}

std::unique_ptr<Leviathan::PhysicsMaterialManager>
ThriveGame::createPhysicsMaterials() const
{
// Setup materials
auto cellMaterial =
std::make_unique<Leviathan::PhysicalMaterial>("cell", 1);
auto floatingOrganelleMaterial =
std::make_unique<Leviathan::PhysicalMaterial>("floatingOrganelle", 2);
auto agentMaterial =
std::make_unique<Leviathan::PhysicalMaterial>("agentCollision", 3);

// Set callbacks //

// Floating organelles
cellMaterial->FormPairWith(*floatingOrganelleMaterial)
.SetCallbacks(nullptr, cellHitFloatingOrganelle);
// Agents
cellMaterial->FormPairWith(*agentMaterial)
.SetCallbacks(agentCallback, nullptr);
// Engulfing
cellMaterial->FormPairWith(*cellMaterial)
.SetCallbacks(cellOnCellAABBHitCallback, cellOnCellActualContact);

auto manager = std::make_unique<Leviathan::PhysicsMaterialManager>();

manager->LoadedMaterialAdd(std::move(cellMaterial));
manager->LoadedMaterialAdd(std::move(floatingOrganelleMaterial));
manager->LoadedMaterialAdd(std::move(agentMaterial));

return manager;
}

void
ThriveGame::EnginePreShutdown()
{
// Shutdown scripting first to allow it to still do anything it wants //
if(m_impl->m_microbeScripts) {
m_impl->m_microbeScripts->ReleaseScript();
m_impl->m_microbeScripts.reset();
}
releaseScripts();

// All resources that need Ogre or the engine to be available when
// they are destroyed need to be released here
Expand Down
Loading

0 comments on commit 7f0f7ee

Please sign in to comment.