Skip to content

Commit

Permalink
Start writing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
grilme99 committed May 5, 2022
1 parent 4ef59ba commit 91d8844
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
12 changes: 12 additions & 0 deletions moonwave.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ href = "https://www.roblox.com/games/8289135181"
label = "Example Game"
position = "right"

[[classOrder]]
section = "Client"
classes = ["ChickynoidClient", "ClientChickynoid", "CharacterModel"]

[[classOrder]]
section = "Server"
classes = ["ChickynoidServer", "ServerChickynoid"]

[[classOrder]]
section = "Common"
classes = ["Simulation", "Types"]

[home]
enabled = true
includeReadme = true
Expand Down
55 changes: 43 additions & 12 deletions src/Client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ ChickynoidClient.startTime = tick()
ChickynoidClient.characters = {}
ChickynoidClient.localFrame = 0
ChickynoidClient.worldState = nil
ChickynoidClient.fpsMax = 144 --Think carefully about changing this! Every extra frame clients make, puts load on the server
ChickynoidClient.fpsIsCapped = true --Dynamically sets to true if your fps is fpsMax + 5
ChickynoidClient.fpsMin = 25 --If you're slower than this, your step will be broken up

ChickynoidClient.cappedElapsedTime = 0 --
ChickynoidClient.timeSinceLastThink = 0
Expand All @@ -52,9 +49,8 @@ ChickynoidClient.stateCounter = 0 --Num states coming in

ChickynoidClient.accumulatedTime = 0

ChickynoidClient.useSubFrameInterpolation = false
ChickynoidClient.fpsIsCapped = true
ChickynoidClient.prevLocalCharacterData = nil
ChickynoidClient.showDebugMovement = true

--This flag can be set to true if we detect we're in a network death spiral, and are going to go quiet for a while
ChickynoidClient.awaitingFullSnapshot = true
Expand All @@ -66,6 +62,29 @@ ChickynoidClient.characterModel = nil
--Milliseconds of *extra* buffer time to account for ping flux
ChickynoidClient.interpolationBuffer = 20

--[=[
@interface ClientConfig
@within ChickynoidClient
.fpsMin number -- If you're slower than this, your step will be broken up.
.fpsMax number -- Think carefully about changing this! Every extra frame clients make, puts load on the server.
.useSubFrameInterpolation bool
.showDebugMovement bool -- Show movement debug in FPS graph.
Client config for Chickynoid.
]=]
ChickynoidClient.config = {
fpsMin = 25,
fpsMax = 144,

useSubFrameInterpolation = false,
showDebugMovement = true,
}

--[=[
Creates connections so that Chickynoid can run on the client. Specfically, it connects to relevant networking and
RunService events.
]=]
function ChickynoidClient:Setup()
local eventHandler = {}

Expand Down Expand Up @@ -145,7 +164,7 @@ function ChickynoidClient:Setup()
--Do a framerate cap to 144? fps
self.cappedElapsedTime += deltaTime
self.timeSinceLastThink += deltaTime
local fraction = 1 / self.fpsMax
local fraction = 1 / self.config.fpsMax

if self.cappedElapsedTime < fraction and self.fpsIsCapped == true then
return --If not enough time for a whole frame has elapsed
Expand Down Expand Up @@ -195,6 +214,12 @@ function ChickynoidClient:Setup()
end)
end

--[=[
Reset the network connection. Used to recover from death spirals by telling the server to stop sending replication
packets.
@private
]=]
function ChickynoidClient:ResetConnection()
if self.awaitingFullSnapshot == false then
--Stop accepting/storing data
Expand All @@ -210,6 +235,12 @@ function ChickynoidClient:ResetConnection()
end
end

--[=[
Calculates FPS and updates the FPS graph. FPS is also used internally for various things.
@param deltaTime number
@private
]=]
function ChickynoidClient:DoFpsCount(deltaTime)
self.frameCounter += 1
self.frameCounterTime += deltaTime
Expand All @@ -220,8 +251,8 @@ function ChickynoidClient:DoFpsCount(deltaTime)
end
--print("FPS: real ", self.frameCounter, "( physics: ",self.frameSimCounter ,")")

if self.frameCounter > self.fpsMax + 5 then
FpsGraph:SetWarning("(Cap your fps to " .. self.fpsMax .. ")")
if self.frameCounter > self.config.fpsMax + 5 then
FpsGraph:SetWarning("(Cap your fps to " .. self.config.fpsMax .. ")")
self.fpsIsCapped = true
else
FpsGraph:SetWarning("")
Expand Down Expand Up @@ -298,7 +329,7 @@ function ChickynoidClient:ProcessFrame(deltaTime)
while self.accumulatedTime > 0 do
self.accumulatedTime -= frac

if self.useSubFrameInterpolation == true then
if self.config.useSubFrameInterpolation == true then
--Todo: could do a small (rarely used) optimization here and only copy the 2nd to last one..
if self.localChickynoid.simulation.characterData ~= nil then
--Capture the state of the client before the current simulation
Expand All @@ -311,7 +342,7 @@ function ChickynoidClient:ProcessFrame(deltaTime)
count += 1
end

if self.useSubFrameInterpolation == true then
if self.config.useSubFrameInterpolation == true then
--if this happens, we have over-simulated
if self.accumulatedTime < 0 then
--we need to do a sub-frame positioning
Expand Down Expand Up @@ -348,7 +379,7 @@ function ChickynoidClient:ProcessFrame(deltaTime)

if self.fixedPhysicsSteps == true then
if
self.useSubFrameInterpolation == false
self.config.useSubFrameInterpolation == false
or subFrameFraction == 0
or self.prevLocalCharacterData == nil
then
Expand All @@ -366,7 +397,7 @@ function ChickynoidClient:ProcessFrame(deltaTime)
self.characterModel:Think(deltaTime, self.localChickynoid.simulation.characterData.serialized)
end

if self.showDebugMovement == true then
if self.config.showDebugMovement == true then
local pos = self.characterModel.model.PrimaryPart.CFrame.Position

if self.previousPos ~= nil then
Expand Down
21 changes: 13 additions & 8 deletions src/Server/ServerChickynoid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ local ServerChickynoid = {}
ServerChickynoid.__index = ServerChickynoid

--[=[
Constructs a new [ServerChickynoid] and attaches it to the specified player.
@return ServerChickynoid
Constructs a new [ServerChickynoid] and attaches it to the specified player.
@param playerRecord any -- The player record.
@return ServerChickynoid
]=]
function ServerChickynoid.new(playerRecord)
local self = setmetatable({
Expand Down Expand Up @@ -116,10 +117,13 @@ function ServerChickynoid:GenerateFakeCommand(deltaTime)
end

--[=[
Steps the simulation forward by one frame. This loop handles the simulation
and replication timings.
]=]
Steps the simulation forward by one frame. This loop handles the simulation
and replication timings.
@param server any -- The server.
@param _serverSimulationTime number -- The server simulation time.
@param deltaTime number -- The DT.
]=]
function ServerChickynoid:Think(server, _serverSimulationTime, deltaTime)
-- Anticheat methods
-- We keep X ms of commands unprocessed, so that if players stop sending upstream, we have some commands to keep going with
Expand Down Expand Up @@ -192,10 +196,11 @@ function ServerChickynoid:Think(server, _serverSimulationTime, deltaTime)
end

--[=[
Callback for handling all events from the client.
Callback for handling all events from the client.
@param event table -- The event sent by the client.
@private
@param server any -- The server.
@param event table -- The event sent by the client.
@private
]=]
function ServerChickynoid:HandleClientEvent(server, event)
if event.t == EventType.Command then
Expand Down
2 changes: 1 addition & 1 deletion src/Server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ end
--[=[
Spawns a new Chickynoid for the specified player.
@param player Player -- The player to spawn this Chickynoid for.
@param playerRecord any -- The player to spawn this Chickynoid for.
@return ServerCharacter -- New chickynoid instance made for this player.
]=]
function ChickynoidServer:CreateChickynoidAsync(playerRecord)
Expand Down
8 changes: 4 additions & 4 deletions src/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local Client = require(script.Client)
local Server = require(script.Server)

local Chickynoid = {
ChickynoidClient = Client,
ChickynoidServer = Server,
}
local Chickynoid = {}

Chickynoid.ChickynoidClient = Client
Chickynoid.ChickynoidServer = Server

return Chickynoid

0 comments on commit 91d8844

Please sign in to comment.