Skip to content

Commit

Permalink
Multiplayer (part 4), minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Nov 1, 2023
1 parent 5b55d95 commit b890262
Show file tree
Hide file tree
Showing 42 changed files with 1,858 additions and 632 deletions.
5 changes: 4 additions & 1 deletion Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand Down Expand Up @@ -344,11 +344,13 @@
<ClInclude Include="Jazz2\LevelDescriptor.h" />
<ClInclude Include="Jazz2\LightEmitter.h" />
<ClInclude Include="Jazz2\Multiplayer\Backends\enet.h" />
<ClInclude Include="Jazz2\Multiplayer\ConnectionResult.h" />
<ClInclude Include="Jazz2\Multiplayer\INetworkHandler.h" />
<ClInclude Include="Jazz2\Multiplayer\MultiLevelHandler.h" />
<ClInclude Include="Jazz2\Multiplayer\NetworkManager.h" />
<ClInclude Include="Jazz2\Multiplayer\PacketTypes.h" />
<ClInclude Include="Jazz2\Multiplayer\Peer.h" />
<ClInclude Include="Jazz2\Multiplayer\Reason.h" />
<ClInclude Include="Jazz2\PitType.h" />
<ClInclude Include="Jazz2\PlayerActions.h" />
<ClInclude Include="Jazz2\PlayerType.h" />
Expand Down Expand Up @@ -709,6 +711,7 @@
<ClCompile Include="Jazz2\Compatibility\JJ2Level.cpp" />
<ClCompile Include="Jazz2\Compatibility\JJ2Strings.cpp" />
<ClCompile Include="Jazz2\Compatibility\JJ2Tileset.cpp" />
<ClCompile Include="Jazz2\Multiplayer\ConnectionResult.cpp" />
<ClCompile Include="Jazz2\Multiplayer\MultiLevelHandler.cpp" />
<ClCompile Include="Jazz2\Multiplayer\NetworkManager.cpp" />
<ClCompile Include="Jazz2\PreferencesCache.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,9 @@
<ClInclude Include="Jazz2\UI\Menu\MenuResources.h">
<Filter>Header Files\Jazz2\UI\Menu</Filter>
</ClInclude>
<ClInclude Include="Jazz2\Multiplayer\Reason.h">
<Filter>Header Files\Jazz2\Multiplayer</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Actors/ActorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ namespace Jazz2::Actors
auto it = _metadata->Sounds.find(String::nullTerminatedView(identifier));
if (it != _metadata->Sounds.end()) {
int idx = (it->second.Buffers.size() > 1 ? Random().Next(0, (int)it->second.Buffers.size()) : 0);
return _levelHandler->PlaySfx(&it->second.Buffers[idx]->Buffer, Vector3f(_pos.X, _pos.Y, 0.0f), false, gain, pitch);
return _levelHandler->PlaySfx(this, identifier, &it->second.Buffers[idx]->Buffer, Vector3f(_pos.X, _pos.Y, 0.0f), false, gain, pitch);
} else {
return nullptr;
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Jazz2/Actors/Environment/Spring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ using namespace Jazz2::Tiles;
namespace Jazz2::Actors::Environment
{
Spring::Spring()
:
_cooldown(0.0f)
: _cooldown(0.0f)
{
}

Expand Down
12 changes: 4 additions & 8 deletions Sources/Jazz2/Actors/Explosion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
namespace Jazz2::Actors
{
Explosion::Explosion()
:
_lightBrightness(0.0f),
_lightIntensity(0.0f),
_lightRadiusNear(0.0f),
_lightRadiusFar(0.0f)
: _lightBrightness(0.0f), _lightIntensity(0.0f), _lightRadiusNear(0.0f), _lightRadiusFar(0.0f)
{
}

void Explosion::Create(ILevelHandler* levelHandler, const Vector3i& pos, Type type)
{
std::shared_ptr<Explosion> explosion = std::make_shared<Explosion>();
uint8_t explosionParams[2];
*(uint16_t*)&explosionParams[0] = (uint16_t)type;
std::uint8_t explosionParams[2];
*(std::uint16_t*)&explosionParams[0] = (uint16_t)type;
explosion->OnActivated(ActorActivationDetails(
levelHandler,
pos,
Expand All @@ -29,7 +25,7 @@ namespace Jazz2::Actors

Task<bool> Explosion::OnActivatedAsync(const ActorActivationDetails& details)
{
_type = (Type)*(uint16_t*)&details.Params[0];
_type = (Type)*(std::uint16_t*)&details.Params[0];

SetState(ActorState::ForceDisableCollisions, true);
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ namespace Jazz2::Actors
auto it = _metadata->Sounds.find(String::nullTerminatedView(identifier));
if (it != _metadata->Sounds.end()) {
int idx = (it->second.Buffers.size() > 1 ? Random().Next(0, (int)it->second.Buffers.size()) : 0);
return _levelHandler->PlaySfx(&it->second.Buffers[idx]->Buffer, Vector3f(0.0f, 0.0f, 0.0f), true, gain, pitch);
return _levelHandler->PlaySfx(this, identifier, &it->second.Buffers[idx]->Buffer, Vector3f(0.0f, 0.0f, 0.0f), true, gain, pitch);
} else {
return nullptr;
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/Jazz2/Actors/RemoteActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ namespace Jazz2::Actors
_stateBuffer[i].Pos = Vector2f(details.Pos.X, details.Pos.Y);
}

async_await RequestMetadataAsync("Interactive/PlayerJazz"_s);

SetAnimation(AnimState::Idle);

async_return true;
}

Expand Down Expand Up @@ -71,6 +67,12 @@ namespace Jazz2::Actors
ActorBase::OnUpdate(timeMult);
}

void RemoteActor::AssignMetadata(const StringView& path, AnimState anim)
{
RequestMetadata(path);
SetAnimation(anim);
}

void RemoteActor::SyncWithServer(const Vector2f& pos, AnimState anim, bool isVisible, bool isFacingLeft)
{
Clock& c = nCine::clock();
Expand Down
1 change: 1 addition & 0 deletions Sources/Jazz2/Actors/RemoteActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Jazz2::Actors
public:
RemoteActor();

void AssignMetadata(const StringView& path, AnimState anim);
void SyncWithServer(const Vector2f& pos, AnimState anim, bool isVisible, bool isFacingLeft);

protected:
Expand Down
9 changes: 5 additions & 4 deletions Sources/Jazz2/ContentResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Jazz2
}

ContentResolver::ContentResolver()
: _isHeadless(false), _isLoading(false), _cachedMetadata(64), _cachedGraphics(128), _palettes{}
: _isHeadless(false), _isLoading(false), _cachedMetadata(64), _cachedGraphics(256), _cachedSounds(192), _palettes{}
{
InitializePaths();
}
Expand Down Expand Up @@ -361,8 +361,8 @@ namespace Jazz2

Metadata* ContentResolver::RequestMetadata(const StringView& path)
{
auto pathNormalized = fs::ToNativeSeparators(path);
auto it = _cachedMetadata.find(String::nullTerminatedView(pathNormalized));
String pathNormalized = fs::ToNativeSeparators(path);
auto it = _cachedMetadata.find(pathNormalized);
if (it != _cachedMetadata.end()) {
// Already loaded - Mark as referenced
it->second->Flags |= MetadataFlags::Referenced;
Expand Down Expand Up @@ -395,6 +395,7 @@ namespace Jazz2
bool multipleAnimsNoStatesWarning = false;

std::unique_ptr<Metadata> metadata = std::make_unique<Metadata>();
metadata->Path = std::move(pathNormalized);
metadata->Flags |= MetadataFlags::Referenced;

ondemand::parser parser;
Expand Down Expand Up @@ -555,7 +556,7 @@ namespace Jazz2
}
}

return _cachedMetadata.emplace(pathNormalized, std::move(metadata)).first->second.get();
return _cachedMetadata.emplace(metadata->Path, std::move(metadata)).first->second.get();
}

GenericGraphicResource* ContentResolver::RequestGraphics(const StringView& path, uint16_t paletteOffset)
Expand Down
10 changes: 9 additions & 1 deletion Sources/Jazz2/ContentResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../nCine/Base/HashMap.h"

#include <Containers/Pair.h>
#include <Containers/Reference.h>
#include <Containers/SmallVector.h>
#include <Containers/StringView.h>
#include <IO/FileSystem.h>
Expand Down Expand Up @@ -84,6 +85,13 @@ namespace Jazz2
}

private:
struct StringRefEqualTo
{
inline bool operator()(const Reference<String>& a, const Reference<String>& b) const noexcept {
return a.get() == b.get();
}
};

ContentResolver();

ContentResolver(const ContentResolver&) = delete;
Expand All @@ -105,7 +113,7 @@ namespace Jazz2
bool _isHeadless;
bool _isLoading;
uint32_t _palettes[PaletteCount * ColorsPerPalette];
HashMap<String, std::unique_ptr<Metadata>> _cachedMetadata;
HashMap<Reference<String>, std::unique_ptr<Metadata>, FNV1aHashFunc<String>, StringRefEqualTo> _cachedMetadata;
HashMap<Pair<String, uint16_t>, std::unique_ptr<GenericGraphicResource>> _cachedGraphics;
HashMap<String, std::unique_ptr<GenericSoundResource>> _cachedSounds;
std::unique_ptr<UI::Font> _fonts[(int32_t)FontType::Count];
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Events/EventMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace Jazz2::Events

void EventMap::ActivateEvents(std::int32_t tx1, std::int32_t ty1, std::int32_t tx2, std::int32_t ty2, bool allowAsync)
{
auto tiles = _levelHandler->TileMap();
auto* tiles = _levelHandler->TileMap();
if (tiles == nullptr) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/ILevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Jazz2

virtual void AddActor(std::shared_ptr<Actors::ActorBase> actor) = 0;

virtual std::shared_ptr<AudioBufferPlayer> PlaySfx(AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain = 1.0f, float pitch = 1.0f) = 0;
virtual std::shared_ptr<AudioBufferPlayer> PlaySfx(Actors::ActorBase* self, const StringView& identifier, AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain = 1.0f, float pitch = 1.0f) = 0;
virtual std::shared_ptr<AudioBufferPlayer> PlayCommonSfx(const StringView& identifier, const Vector3f& pos, float gain = 1.0f, float pitch = 1.0f) = 0;
virtual void WarpCameraToTarget(const std::shared_ptr<Actors::ActorBase>& actor, bool fast = false) = 0;
virtual bool IsPositionEmpty(Actors::ActorBase* self, const AABBf& aabb, Tiles::TileCollisionParams& params, Actors::ActorBase** collider) = 0;
Expand Down
98 changes: 55 additions & 43 deletions Sources/Jazz2/LevelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,46 +314,7 @@ namespace Jazz2
}
}

if (/*_difficulty != GameDifficulty::Multiplayer*/true) {
if (!_players.empty()) {
auto& pos = _players[0]->GetPos();
int32_t tx1 = (int32_t)pos.X / Tiles::TileSet::DefaultTileSize;
int32_t ty1 = (int32_t)pos.Y / Tiles::TileSet::DefaultTileSize;
int32_t tx2 = tx1;
int32_t ty2 = ty1;

tx1 -= ActivateTileRange;
ty1 -= ActivateTileRange;
tx2 += ActivateTileRange;
ty2 += ActivateTileRange;

int32_t tx1d = tx1 - 4;
int32_t ty1d = ty1 - 4;
int32_t tx2d = tx2 + 4;
int32_t ty2d = ty2 + 4;

for (auto& actor : _actors) {
if ((actor->_state & (Actors::ActorState::IsCreatedFromEventMap | Actors::ActorState::IsFromGenerator)) != Actors::ActorState::None) {
Vector2i originTile = actor->_originTile;
if (originTile.X < tx1d || originTile.Y < ty1d || originTile.X > tx2d || originTile.Y > ty2d) {
if (actor->OnTileDeactivated()) {
if ((actor->_state & Actors::ActorState::IsFromGenerator) == Actors::ActorState::IsFromGenerator) {
_eventMap->ResetGenerator(originTile.X, originTile.Y);
}

_eventMap->Deactivate(originTile.X, originTile.Y);

actor->_state |= Actors::ActorState::IsDestroyed;
}
}
}
}

_eventMap->ActivateEvents(tx1, ty1, tx2, ty2, true);
}

_eventMap->ProcessGenerators(timeMult);
}
ProcessEvents(timeMult);

// Weather
if (_weatherType != WeatherType::None) {
Expand Down Expand Up @@ -472,7 +433,7 @@ namespace Jazz2
{
float timeMult = theApplication().timeMult();

if (_pauseMenu == nullptr) {
if (!IsPausable() || _pauseMenu == nullptr) {
ResolveCollisions(timeMult);

// Ambient Light Transition
Expand Down Expand Up @@ -702,7 +663,7 @@ namespace Jazz2
_actors.emplace_back(actor);
}

std::shared_ptr<AudioBufferPlayer> LevelHandler::PlaySfx(AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain, float pitch)
std::shared_ptr<AudioBufferPlayer> LevelHandler::PlaySfx(Actors::ActorBase* self, const StringView& identifier, AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain, float pitch)
{
auto& player = _playingSounds.emplace_back(std::make_shared<AudioBufferPlayer>(buffer));
player->setPosition(Vector3f(pos.X, pos.Y, 100.0f));
Expand Down Expand Up @@ -1279,17 +1240,68 @@ namespace Jazz2
}
}

void LevelHandler::BeforeActorDestroyed(Actors::ActorBase* actor)
{
// Nothing to do here
}

void LevelHandler::ProcessEvents(float timeMult)
{
if (!_players.empty()) {
std::size_t playerCount = _players.size();
SmallVector<AABBi, 2> playerZones;
playerZones.reserve(playerCount * 2);
for (std::size_t i = 0; i < playerCount; i++) {
auto pos = _players[i]->GetPos();
std::int32_t tx = (std::int32_t)pos.X / TileSet::DefaultTileSize;
std::int32_t ty = (std::int32_t)pos.Y / TileSet::DefaultTileSize;

const auto& activationRange = playerZones.emplace_back(tx - ActivateTileRange, ty - ActivateTileRange, tx + ActivateTileRange, ty + ActivateTileRange);
playerZones.emplace_back(activationRange.L - 4, activationRange.T - 4, activationRange.R + 4, activationRange.B + 4);
}

for (auto& actor : _actors) {
if ((actor->_state & (Actors::ActorState::IsCreatedFromEventMap | Actors::ActorState::IsFromGenerator)) != Actors::ActorState::None) {
Vector2i originTile = actor->_originTile;
bool isInside = false;
for (std::size_t i = 1; i < playerZones.size(); i += 2) {
if (playerZones[i].Contains(originTile)) {
isInside = true;
break;
}
}

if (!isInside && actor->OnTileDeactivated()) {
if ((actor->_state & Actors::ActorState::IsFromGenerator) == Actors::ActorState::IsFromGenerator) {
_eventMap->ResetGenerator(originTile.X, originTile.Y);
}

_eventMap->Deactivate(originTile.X, originTile.Y);
actor->_state |= Actors::ActorState::IsDestroyed;
}
}
}

for (std::size_t i = 0; i < playerZones.size(); i += 2) {
const auto& activationZone = playerZones[i];
_eventMap->ActivateEvents(activationZone.L, activationZone.T, activationZone.R, activationZone.B, true);
}
}

_eventMap->ProcessGenerators(timeMult);
}

void LevelHandler::ResolveCollisions(float timeMult)
{
auto it = _actors.begin();
while (it != _actors.end()) {
Actors::ActorBase* actor = it->get();
if (actor->GetState(Actors::ActorState::IsDestroyed)) {
BeforeActorDestroyed(actor);
if (actor->CollisionProxyID != Collisions::NullNode) {
_collisions.DestroyProxy(actor->CollisionProxyID);
actor->CollisionProxyID = Collisions::NullNode;
}

it = _actors.erase(it);
continue;
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/Jazz2/LevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace Jazz2

void AddActor(std::shared_ptr<Actors::ActorBase> actor) override;

std::shared_ptr<AudioBufferPlayer> PlaySfx(AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain = 1.0f, float pitch = 1.0f) override;
std::shared_ptr<AudioBufferPlayer> PlaySfx(Actors::ActorBase* self, const StringView& identifier, AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain, float pitch) override;
std::shared_ptr<AudioBufferPlayer> PlayCommonSfx(const StringView& identifier, const Vector3f& pos, float gain = 1.0f, float pitch = 1.0f) override;
void WarpCameraToTarget(const std::shared_ptr<Actors::ActorBase>& actor, bool fast = false) override;
bool IsPositionEmpty(Actors::ActorBase* self, const AABBf& aabb, Tiles::TileCollisionParams& params, Actors::ActorBase** collider) override;
Expand Down Expand Up @@ -309,6 +309,9 @@ namespace Jazz2
bool _playerFrozenEnabled;
uint32_t _lastPressedNumericKey;

virtual void BeforeActorDestroyed(Actors::ActorBase* actor);
virtual void ProcessEvents(float timeMult);

void ResolveCollisions(float timeMult);
void InitializeCamera();
void UpdateCamera(float timeMult);
Expand Down
25 changes: 25 additions & 0 deletions Sources/Jazz2/Multiplayer/ConnectionResult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ConnectionResult.h"

#if defined(WITH_MULTIPLAYER)

#include "../../Common.h"

namespace Jazz2::Multiplayer
{
ConnectionResult::ConnectionResult(Reason reason)
: FailureReason(reason)
{
}

ConnectionResult::ConnectionResult(bool success)
: FailureReason(success ? (Reason)UINT32_MAX : Reason::Unknown)
{
}

bool ConnectionResult::IsSuccessful() const
{
return FailureReason == (Reason)UINT32_MAX;
}
}

#endif
Loading

0 comments on commit b890262

Please sign in to comment.