Files
RP412/MUNGA/UPDATE.cpp
T
CydandClaude Opus 5 6e829f815e Doors run on the mission clock instead of being replicated
A door's position was an integrated countdown owned by whichever machine
the map-entity round-robin happened to deal it to. That left doors one
one-way-latency behind on every other machine, re-acquired at each state
change; drifting permanently on any frame hitch over a second, which the
old code dropped outright rather than clamping; and frozen mid-cycle,
collision volumes included, when their owning peer left, since ownership
transfer is not implemented.

Doors are clockwork with no inputs, and door/VTV physics is already local
pointer access - VTV::ProcessCollision reads door->currentVelocity off
the local object and the crush test is local VTV state - so a door does
not need an owner at all. Door::SlideDoor is now a phase function of
Application::GetMissionElapsed(), anchored so phase zero reproduces the
original DefaultState entry: fully open, starting to close. Every host
builds its own doorframe out of the map stream as a HermitInstance, the
instance kind DynamicEntityCreation does not broadcast, so nothing is
sent, nothing is received, and a peer leaving takes no doors with it.

Verified against a copy of the old integrator at 25fps with the real 10s
travel / 3s dead timings: identical 26s cycle, a constant one-frame
offset, and no drift across a 3s stall that leaves the old code
permanently 3 seconds out of phase.

Also fixes a latent bug found on the way: UpdateManager iterates the
dynamic master socket, which holds Independant and Hermit instances as
well as masters, and handed all of them to EntityUpdateReplicants, which
asserts MasterInstance.

Doorframes no longer consume a slot in the map-entity ownership cursor,
which shifts who owns every map entity dealt after them, so this cannot
share a session with an older build. The lobby publishes a simulation
revision and refuses to launch a mixed room.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 16:25:42 -05:00

253 lines
6.8 KiB
C++

#include "munga.h"
#pragma hdrstop
#include "update.h"
#include "app.h"
#include "hostmgr.h"
#include "interest.h"
#include "entity.h"
#if defined(TRACE_UPDATE_REPLICANTS)
static BitTrace Update_Replicants("Update Replicants");
#define SET_UPDATE_REPLICANTS() Update_Replicants.Set()
#define CLEAR_UPDATE_REPLICANTS() Update_Replicants.Clear()
#else
#define SET_UPDATE_REPLICANTS()
#define CLEAR_UPDATE_REPLICANTS()
#endif
#if defined(TRACE_UPDATE_MASTERS)
static BitTrace Update_Masters("Update Masters");
#define SET_UPDATE_MASTERS() Update_Masters.Set()
#define CLEAR_UPDATE_MASTERS() Update_Masters.Clear()
#else
#define SET_UPDATE_MASTERS()
#define CLEAR_UPDATE_MASTERS()
#endif
MemoryBlock *SimulationEncore::GetAllocatedMemory()
{
static MemoryBlock allocatedMemory(sizeof(SimulationEncore), 10, 10, "Simulation Encores");
return &allocatedMemory;
}
SimulationEncore::SimulationEncore(
Simulation *sim,
Simulation::Encore callback
)
{
encoreSimulation = sim;
encoreCallback = callback;
runWatchers = True;
}
SimulationEncore::~SimulationEncore()
{
}
//
//#############################################################################
//#############################################################################
//
UpdateManager::UpdateManager():
simulationEncores(NULL)
{
}
//
//#############################################################################
//#############################################################################
//
UpdateManager::~UpdateManager()
{
ChainIteratorOf<SimulationEncore*> encores(simulationEncores);
SimulationEncore *encore;
while ((encore = encores.ReadAndNext()) != NULL)
{
Unregister_Object(encore);
delete encore;
}
}
//
//#############################################################################
//#############################################################################
//
Logical
UpdateManager::TestInstance() const
{
Node::TestInstance();
return True;
}
//
//#############################################################################
//#############################################################################
//
void
UpdateManager::Execute(Time target_render_time)
{
Check(this);
//
//--------------------------------------------------------------------------
// Execute the replicants and masters that are interesting and dynamic
//--------------------------------------------------------------------------
//
Check(application);
Check(application->GetHostManager());
HostManager::DynamicReplicantEntityIterator
replicant_entity_iterator(application->GetHostManager());
HostManager::DynamicMasterEntityIterator
master_entity_iterator(application->GetHostManager());
//
//--------------------------------------------------------------------------
// Execute the replicants
//--------------------------------------------------------------------------
//
SET_UPDATE_REPLICANTS();
Entity
*replicant;
while ((replicant = replicant_entity_iterator.ReadAndNext()) != NULL)
{
Check(replicant);
if (replicant->IsReplicantExecutable())
{
replicant->Execute(target_render_time);
}
}
CLEAR_UPDATE_REPLICANTS();
//
//--------------------------------------------------------------------------
// Execute the master entities, if they need to be updated then send
// update messages, and notify the interest manager of moved entities
//--------------------------------------------------------------------------
//
SET_UPDATE_MASTERS();
Entity
*entity;
Entity::UpdateMessage
*update_message;
while ((entity = master_entity_iterator.ReadAndNext()) != NULL)
{
Check(entity);
//
//-----------------------------------------------------------------------
// If the entity is not interesting then skip it
//-----------------------------------------------------------------------
//
if (!entity->IsInteresting() || !entity->IsNonReplicantExecutable())
{
continue;
}
//
//-----------------------------------------------------------------------
// Run the execute method
//-----------------------------------------------------------------------
//
update_message = entity->Execute(target_render_time);
//
//-----------------------------------------------------------------------
// Update the interest zone id
//-----------------------------------------------------------------------
//
Check(application);
Check(application->GetInterestManager());
application->GetInterestManager()->EntityUpdateInterestZone(entity);
//
//-----------------------------------------------------------------------
// If update message is not null then send the change.
//
// The dynamic master socket holds Independant and Hermit instances as
// well as masters, and neither of those publishes: an Independant runs
// its own simulation on every host, and a Hermit is not replicated at
// all. EntityUpdateReplicants asserts MasterInstance, so the caller is
// the one that has to make that true - the clockwork doorframes are
// Hermits and would otherwise arrive there.
//-----------------------------------------------------------------------
//
if (
update_message != NULL
&& entity->GetInstance() == Entity::MasterInstance
)
{
Check(update_message);
Check(application);
application->GetInterestManager()->EntityUpdateReplicants(
entity,
update_message
);
}
}
//
//--------------------------------------------------------------------------
// Lastly, run through and do any requested encores
//--------------------------------------------------------------------------
//
ChainIteratorOf<SimulationEncore*>
encores(simulationEncores);
SimulationEncore
*encore;
while ((encore = encores.ReadAndNext()) != NULL)
{
Check(encore);
Check(encore->encoreSimulation);
(encore->encoreSimulation->*encore->encoreCallback)();
if (encore->runWatchers)
{
encore->encoreSimulation->ExecuteWatchers();
encore->encoreSimulation->ClearWatcherDelay();
}
Unregister_Object(encore);
delete encore;
}
CLEAR_UPDATE_MASTERS();
}
//
//#############################################################################
//#############################################################################
//
void
UpdateManager::RequestEncore(
Simulation *simulation,
Simulation::Encore encore
)
{
//
//----------------------------------------------------------------------
// Make sure that we only run the watcher on a given simulation once per
// frame
//----------------------------------------------------------------------
//
ChainIteratorOf<SimulationEncore*> encores(simulationEncores);
SimulationEncore *i;
while ((i = encores.ReadAndNext()) != NULL)
{
if (i->encoreSimulation == simulation && i->runWatchers)
{
i->runWatchers = False;
break;
}
}
SimulationEncore *request = new SimulationEncore(simulation, encore);
Register_Object(request);
simulationEncores.Add(request);
}