osb/source/game/StarWorldClient.hpp

382 lines
14 KiB
C++
Raw Normal View History

#pragma once
2023-06-20 04:33:09 +00:00
#include "StarWorldClientState.hpp"
#include "StarNetPackets.hpp"
#include "StarWorldRenderData.hpp"
#include "StarAmbient.hpp"
#include "StarCellularLighting.hpp"
#include "StarWeather.hpp"
#include "StarInterpolationTracker.hpp"
#include "StarWorldStructure.hpp"
#include "StarChatAction.hpp"
#include "StarWiring.hpp"
#include "StarEntityRendering.hpp"
#include "StarWorld.hpp"
#include "StarGameTimers.hpp"
#include "StarLuaRoot.hpp"
#include "StarTickRateMonitor.hpp"
2023-06-20 04:33:09 +00:00
namespace Star {
STAR_STRUCT(Biome);
STAR_CLASS(WorldTemplate);
STAR_CLASS(Sky);
STAR_CLASS(Parallax);
STAR_CLASS(LuaRoot);
STAR_CLASS(DamageManager);
STAR_CLASS(EntityMap);
STAR_CLASS(ParticleManager);
STAR_CLASS(WorldClient);
STAR_CLASS(Player);
STAR_CLASS(Item);
STAR_CLASS(CelestialLog);
STAR_CLASS(ClientContext);
STAR_CLASS(PlayerStorage);
STAR_STRUCT(OverheadBar);
STAR_EXCEPTION(WorldClientException, StarException);
class WorldClient : public World {
public:
WorldClient(PlayerPtr mainPlayer);
~WorldClient();
ConnectionId connection() const override;
WorldGeometry geometry() const override;
uint64_t currentStep() const override;
MaterialId material(Vec2I const& position, TileLayer layer) const override;
MaterialHue materialHueShift(Vec2I const& position, TileLayer layer) const override;
ModId mod(Vec2I const& position, TileLayer layer) const override;
MaterialHue modHueShift(Vec2I const& position, TileLayer layer) const override;
MaterialColorVariant colorVariant(Vec2I const& position, TileLayer layer) const override;
LiquidLevel liquidLevel(Vec2I const& pos) const override;
LiquidLevel liquidLevel(RectF const& region) const override;
TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override;
TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override;
EntityPtr entity(EntityId entityId) const override;
void addEntity(EntityPtr const& entity, EntityId entityId = NullEntityId) override;
2023-06-20 04:33:09 +00:00
EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = EntityFilter()) const override;
void forAllEntities(EntityCallback entityCallback) const override;
void forEachEntity(RectF const& boundBox, EntityCallback callback) const override;
void forEachEntityLine(Vec2F const& begin, Vec2F const& end, EntityCallback callback) const override;
void forEachEntityAtTile(Vec2I const& pos, EntityCallbackOf<TileEntity> entityCallback) const override;
EntityPtr findEntity(RectF const& boundBox, EntityFilter entityFilter) const override;
EntityPtr findEntityLine(Vec2F const& begin, Vec2F const& end, EntityFilter entityFilter) const override;
EntityPtr findEntityAtTile(Vec2I const& pos, EntityFilterOf<TileEntity> entityFilter) const override;
bool tileIsOccupied(Vec2I const& pos, TileLayer layer, bool includeEphemeral = false, bool checkCollision = false) const override;
CollisionKind tileCollisionKind(Vec2I const& pos) const override;
2023-06-20 04:33:09 +00:00
void forEachCollisionBlock(RectI const& region, function<void(CollisionBlock const&)> const& iterator) const override;
bool isTileConnectable(Vec2I const& pos, TileLayer layer, bool tilesOnly = false) const override;
bool pointTileCollision(Vec2F const& point, CollisionSet const& collisionSet = DefaultCollisionSet) const override;
bool lineTileCollision(Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet = DefaultCollisionSet) const override;
Maybe<pair<Vec2F, Vec2I>> lineTileCollisionPoint(Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet = DefaultCollisionSet) const override;
List<Vec2I> collidingTilesAlongLine(Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet = DefaultCollisionSet, int maxSize = -1, bool includeEdges = true) const override;
bool rectTileCollision(RectI const& region, CollisionSet const& collisionSet = DefaultCollisionSet) const override;
TileDamageResult damageTiles(List<Vec2I> const& pos, TileLayer layer, Vec2F const& sourcePosition, TileDamage const& tileDamage, Maybe<EntityId> sourceEntity = {}) override;
InteractiveEntityPtr getInteractiveInRange(Vec2F const& targetPosition, Vec2F const& sourcePosition, float maxRange) const override;
bool canReachEntity(Vec2F const& position, float radius, EntityId targetEntity, bool preferInteractive = true) const override;
RpcPromise<InteractAction> interact(InteractRequest const& request) override;
float gravity(Vec2F const& pos) const override;
float windLevel(Vec2F const& pos) const override;
float lightLevel(Vec2F const& pos) const override;
bool breathable(Vec2F const& pos) const override;
float threatLevel() const override;
StringList environmentStatusEffects(Vec2F const& pos) const override;
StringList weatherStatusEffects(Vec2F const& pos) const override;
bool exposedToWeather(Vec2F const& pos) const override;
bool isUnderground(Vec2F const& pos) const override;
bool disableDeathDrops() const override;
List<PhysicsForceRegion> forceRegions() const override;
Json getProperty(String const& propertyName, Json const& def = Json()) const override;
void setProperty(String const& propertyName, Json const& property) override;
void timer(float delay, WorldAction worldAction) override;
2023-06-20 04:33:09 +00:00
double epochTime() const override;
uint32_t day() const override;
float dayLength() const override;
float timeOfDay() const override;
LuaRootPtr luaRoot() override;
RpcPromise<Vec2F> findUniqueEntity(String const& uniqueId) override;
RpcPromise<Json> sendEntityMessage(Variant<EntityId, String> const& entity, String const& message, JsonArray const& args = {}) override;
bool isTileProtected(Vec2I const& pos) const override;
// Is this WorldClient properly initialized in a world
bool inWorld() const;
bool inSpace() const;
bool flying() const;
bool mainPlayerDead() const;
void reviveMainPlayer();
bool respawnInWorld() const;
2024-09-09 10:32:23 +00:00
void setRespawnInWorld(bool respawnInWorld);
2023-07-22 12:31:04 +00:00
void removeEntity(EntityId entityId, bool andDie);
2023-06-20 04:33:09 +00:00
WorldTemplateConstPtr currentTemplate() const;
SkyConstPtr currentSky() const;
void dimWorld();
bool interactiveHighlightMode() const;
void setInteractiveHighlightMode(bool enabled);
void setParallax(ParallaxPtr newParallax);
void overrideGravity(float gravity);
void resetGravity();
// Disable normal client-side lighting algorithm, everything full brightness.
2023-11-24 09:35:45 +00:00
bool fullBright() const;
void setFullBright(bool fullBright);
// Disable asynchronous client-side lighting algorithm, run on main thread.
2023-11-24 09:35:45 +00:00
bool asyncLighting() const;
void setAsyncLighting(bool asyncLighting);
2023-06-20 04:33:09 +00:00
// Spatial log generated collision geometry.
2023-11-24 09:35:45 +00:00
bool collisionDebug() const;
void setCollisionDebug(bool collisionDebug);
2023-06-20 04:33:09 +00:00
void handleIncomingPackets(List<PacketPtr> const& packets);
List<PacketPtr> getOutgoingPackets();
// Sets default callbacks in the LuaRoot.
void setLuaCallbacks(String const& groupName, LuaCallbacks const& callbacks);
2023-06-20 04:33:09 +00:00
// Set the rendering window for this client.
void setClientWindow(RectI window);
// Sets the client window around the position of the main player.
void centerClientWindowOnPlayer(Vec2U const& windowSize);
void centerClientWindowOnPlayer();
RectI clientWindow() const;
2024-04-04 03:35:36 +00:00
WorldClientState& clientState();
2023-06-20 04:33:09 +00:00
void update(float dt);
2023-06-20 04:33:09 +00:00
// borderTiles here should extend the client window for border tile
// calculations. It is not necessary on the light array.
void render(WorldRenderData& renderData, unsigned borderTiles);
List<AudioInstancePtr> pullPendingAudio();
List<AudioInstancePtr> pullPendingMusic();
bool playerCanReachEntity(EntityId entityId, bool preferInteractive = true) const;
void disconnectAllWires(Vec2I wireEntityPosition, WireNode const& node);
void connectWire(WireConnection const& output, WireConnection const& input);
2023-07-13 07:58:35 +00:00
// Functions for sending broadcast messages to other players that can receive them,
// on completely vanilla servers by smuggling it through a DamageNotification.
// It's cursed as fuck, but it works.
bool sendSecretBroadcast(StringView broadcast, bool raw = false, bool compress = true);
2023-07-13 07:58:35 +00:00
bool handleSecretBroadcast(PlayerPtr player, StringView broadcast);
2023-06-20 04:33:09 +00:00
List<ChatAction> pullPendingChatActions();
WorldStructure const& centralStructure() const;
DungeonId dungeonId(Vec2I const& pos) const;
void collectLiquid(List<Vec2I> const& tilePositions, LiquidId liquidId);
2024-03-19 14:53:34 +00:00
bool waitForLighting(WorldRenderData* renderData = nullptr);
2023-07-15 04:01:44 +00:00
typedef std::function<bool(PlayerPtr, StringView)> BroadcastCallback;
BroadcastCallback& broadcastCallback();
2023-06-20 04:33:09 +00:00
private:
static const float DropDist;
struct ClientRenderCallback : RenderCallback {
void addDrawable(Drawable drawable, EntityRenderLayer renderLayer) override;
void addLightSource(LightSource lightSource) override;
void addParticle(Particle particle) override;
void addAudio(AudioInstancePtr audio) override;
void addTilePreview(PreviewTile preview) override;
void addOverheadBar(OverheadBar bar) override;
Map<EntityRenderLayer, List<Drawable>> drawables;
List<LightSource> lightSources;
List<Particle> particles;
List<AudioInstancePtr> audios;
List<PreviewTile> previewTiles;
List<OverheadBar> overheadBars;
};
struct DamageNumber {
float amount;
Vec2F position;
double timestamp;
};
struct DamageNumberKey {
String damageNumberParticleKind;
EntityId sourceEntityId;
EntityId targetEntityId;
bool operator<(DamageNumberKey const& other) const;
};
2023-07-30 16:40:06 +00:00
typedef function<ClientTile const& (Vec2I)> ClientTileGetter;
2023-06-29 18:34:10 +00:00
void lightingTileGather();
2024-03-19 07:21:54 +00:00
void lightingCalc();
void lightingMain();
2023-06-20 04:33:09 +00:00
void initWorld(WorldStartPacket const& packet);
void clearWorld();
2024-03-11 06:03:09 +00:00
void tryGiveMainPlayerItem(ItemPtr item, bool silent = false);
2023-06-20 04:33:09 +00:00
void notifyEntityCreate(EntityPtr const& entity);
2023-06-20 04:33:09 +00:00
// Queues pending (step based) updates to server,
void queueUpdatePackets(bool sendEntityUpdates);
2023-06-20 04:33:09 +00:00
void handleDamageNotifications();
void sparkDamagedBlocks();
Vec2I environmentBiomeTrackPosition() const;
AmbientNoisesDescriptionPtr currentAmbientNoises() const;
WeatherNoisesDescriptionPtr currentWeatherNoises() const;
AmbientNoisesDescriptionPtr currentMusicTrack() const;
AmbientNoisesDescriptionPtr currentAltMusicTrack() const;
void playAltMusic(StringList const& newTracks, float fadeTime);
void stopAltMusic(float fadeTime);
BiomeConstPtr mainEnvironmentBiome() const;
// Populates foregroundTransparent / backgroundTransparent flag on ClientTile
// based on transparency rules.
2023-08-19 02:55:32 +00:00
bool readNetTile(Vec2I const& pos, NetTile const& netTile, bool updateCollision = true);
2023-06-20 04:33:09 +00:00
void dirtyCollision(RectI const& region);
void freshenCollision(RectI const& region);
void renderCollisionDebug();
void informTilePrediction(Vec2I const& pos, TileModification const& modification);
2023-07-30 16:40:06 +00:00
2023-06-20 04:33:09 +00:00
void setTileProtection(DungeonId dungeonId, bool isProtected);
void setupForceRegions();
Json m_clientConfig;
WorldTemplatePtr m_worldTemplate;
WorldStructure m_centralStructure;
Vec2F m_playerStart;
bool m_respawnInWorld;
JsonObject m_worldProperties;
EntityMapPtr m_entityMap;
ClientTileSectorArrayPtr m_tileArray;
2023-07-30 16:40:06 +00:00
ClientTileGetter m_tileGetterFunction;
2023-06-20 04:33:09 +00:00
DamageManagerPtr m_damageManager;
LuaRootPtr m_luaRoot;
WorldGeometry m_geometry;
uint64_t m_currentStep;
double m_currentTime;
2023-06-20 04:33:09 +00:00
bool m_fullBright;
bool m_asyncLighting;
2023-06-20 04:33:09 +00:00
CellularLightingCalculator m_lightingCalculator;
mutable CellularLightIntensityCalculator m_lightIntensityCalculator;
ThreadFunction<void> m_lightingThread;
Mutex m_lightingMutex;
ConditionVariable m_lightingCond;
2024-03-19 07:21:54 +00:00
atomic<bool> m_stopLightingThread;
Mutex m_lightMapPrepMutex;
Mutex m_lightMapMutex;
2024-03-19 07:21:54 +00:00
2024-03-19 14:53:34 +00:00
Lightmap m_pendingLightMap;
Lightmap m_lightMap;
2024-03-19 07:21:54 +00:00
List<LightSource> m_pendingLights;
2024-03-19 14:53:34 +00:00
List<std::pair<Vec2F, Vec3F>> m_pendingParticleLights;
2024-03-19 07:21:54 +00:00
RectI m_pendingLightRange;
Vec2I m_lightMinPosition;
List<PreviewTile> m_previewTiles;
2023-06-20 04:33:09 +00:00
SkyPtr m_sky;
CollisionGenerator m_collisionGenerator;
WorldClientState m_clientState;
Maybe<ConnectionId> m_clientId;
PlayerPtr m_mainPlayer;
bool m_collisionDebug;
// Client side entity updates are not done until m_inWorld is true, which is
// set to true after we have entered a world *and* the first batch of updates
// are received.
bool m_inWorld;
GameTimer m_worldDimTimer;
float m_worldDimLevel;
Vec3B m_worldDimColor;
bool m_interactiveHighlightMode;
GameTimer m_parallaxFadeTimer;
ParallaxPtr m_currentParallax;
ParallaxPtr m_nextParallax;
Maybe<float> m_overrideGravity;
ClientWeather m_weather;
ParticleManagerPtr m_particles;
List<AudioInstancePtr> m_samples;
List<AudioInstancePtr> m_music;
HashMap<EntityId, uint64_t> m_masterEntitiesNetVersion;
InterpolationTracker m_interpolationTracker;
GameTimer m_entityUpdateTimer;
2023-06-20 04:33:09 +00:00
List<PacketPtr> m_outgoingPackets;
Maybe<int64_t> m_pingTime;
int64_t m_latency;
Set<EntityId> m_requestedDrops;
Particle m_blockDamageParticle;
Particle m_blockDamageParticleVariance;
float m_blockDamageParticleProbability;
Particle m_blockDingParticle;
Particle m_blockDingParticleVariance;
float m_blockDingParticleProbability;
HashSet<Vec2I> m_damagedBlocks;
AmbientManager m_ambientSounds;
AmbientManager m_musicTrack;
AmbientManager m_altMusicTrack;
List<pair<float, WorldAction>> m_timers;
2023-06-20 04:33:09 +00:00
Map<DamageNumberKey, DamageNumber> m_damageNumbers;
float m_damageNotificationBatchDuration;
AudioInstancePtr m_spaceSound;
String m_activeSpaceSound;
AmbientNoisesDescriptionPtr m_altMusicTrackDescription;
bool m_altMusicActive;
int m_modifiedTilePredictionTimeout;
2023-07-30 16:40:06 +00:00
HashMap<Vec2I, PredictedTile> m_predictedTiles;
2023-06-20 04:33:09 +00:00
HashSet<EntityId> m_startupHiddenEntities;
HashMap<DungeonId, float> m_dungeonIdGravity;
HashMap<DungeonId, bool> m_dungeonIdBreathable;
Set<DungeonId> m_protectedDungeonIds;
HashMap<String, List<RpcPromiseKeeper<Vec2F>>> m_findUniqueEntityResponses;
HashMap<Uuid, RpcPromiseKeeper<Json>> m_entityMessageResponses;
HashMap<Uuid, RpcPromiseKeeper<InteractAction>> m_entityInteractionResponses;
List<PhysicsForceRegion> m_forceRegions;
2023-07-15 04:01:44 +00:00
BroadcastCallback m_broadcastCallback;
2023-06-20 04:33:09 +00:00
};
}