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

Feature: AI can be used in online multiplayer #30

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Rend 0.11.0 changelog:

Gameplay:
* AI can be used in online multiplayer

Rend 0.10.0 changelog:

Gameplay:
Expand Down
23 changes: 17 additions & 6 deletions src/lib-app/src/app/AppStateIngame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,23 @@ void AppStateIngame::simulateFrameFromState(
const FrameState& state, bool skipAudio)
{
restoreState(state);
gameLoop->update(FRAME_TIME, app.time.getDeltaTime(), skipAudio);
gameLoop->update(
FRAME_TIME,
app.time.getDeltaTime(),
skipAudio,
[&]
{
// This gets executed after aiEngine.update
// propagate AI inputs to playerStates::input
// so NPCs do their thing
for (auto&& i : std::views::iota(firstNpcIdx, state.inputs.size()))
{
scene.playerStates[i].input.deserializeFrom(
scene.playerStates[i]
.blackboard.value()
.input->getSnapshot());
}
});
++scene.tick;
}

Expand All @@ -327,11 +343,6 @@ void AppStateIngame::restoreState(const FrameState& state)
scene.playerStates[i].input.deserializeFrom(state.inputs[i]);
}

for (auto&& i : std::views::iota(firstNpcIdx, state.inputs.size()))
{
scene.playerStates[i].input.deserializeFrom(inputs[i]->getSnapshot());
}

scene.camera.anchorIdx = state.cameraAnchorIdx;

// rebuild spatial index
Expand Down
13 changes: 12 additions & 1 deletion src/lib-game/include/GameLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ class [[nodiscard]] GameLoop final
}

public:
void update(const float dt, const float realDt, bool skipAudio);
void update(
const float dt,
const float realDt,
bool skipAudio,
auto&& afterAiEngineUpdateCallback)
{
aiEngine.update(dt);
afterAiEngineUpdateCallback();
updateEngines(dt, realDt);
processEvents(skipAudio);
gameRulesEngine.deleteMarkedObjects();
}

void renderTo(dgm::Window& window);

Expand Down
8 changes: 0 additions & 8 deletions src/lib-game/src/GameLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include "GameLoop.hpp"

void GameLoop::update(const float dt, const float realDt, bool skipAudio)
{
updateEngines(dt, realDt);
processEvents(skipAudio);
gameRulesEngine.deleteMarkedObjects();
aiEngine.update(dt); // must happen after everything else
}

void GameLoop::renderTo(dgm::Window& window)
{
renderingEngine.renderTo(window);
Expand Down
Loading