Fixed Network Area Timer system

This commit is contained in:
MysterD 2021-08-03 18:02:21 -07:00
parent 95fa0c626e
commit a520b25c26
8 changed files with 38 additions and 38 deletions

View file

@ -259,13 +259,11 @@ void (*sBowserPuzzlePieceActions[])(void) = {
}; };
void bhv_lll_bowser_puzzle_piece_loop(void) { void bhv_lll_bowser_puzzle_piece_loop(void) {
// make sure we're loaded and synchronized u32 loopLength = 650;
if (!gNetworkAreaLoaded) { if ((gNetworkAreaTimer - o->oBowserPuzzlePieceTimer) >= loopLength) {
o->oTimer = 0; u32 catchup = (gNetworkAreaTimer - o->oBowserPuzzlePieceTimer) / loopLength;
return; catchup *= loopLength;
} else if (o->oBowserPuzzlePieceTimer == 0 && (gNetworkAreaTimer - o->oBowserPuzzlePieceTimer) >= 650) { o->oBowserPuzzlePieceTimer += catchup;
o->oBowserPuzzlePieceTimer = ((gNetworkAreaTimer - o->oBowserPuzzlePieceTimer) / 650) * 650;
o->oTimer = 0;
} }
while (o->oBowserPuzzlePieceTimer < gNetworkAreaTimer) { while (o->oBowserPuzzlePieceTimer < gNetworkAreaTimer) {

View file

@ -54,17 +54,11 @@ void bhv_checkerboard_platform_init(void) {
} }
void bhv_checkerboard_platform_loop(void) { void bhv_checkerboard_platform_loop(void) {
// make sure we're loaded and synchronized
if (!gNetworkAreaLoaded) {
o->oTimer = 0;
o->oCheckerBoardPlatformTimer = 0;
return;
} else {
u32 loopLength = 132 + o->oCheckerBoardPlatformUnkFC * 2; u32 loopLength = 132 + o->oCheckerBoardPlatformUnkFC * 2;
if (o->oCheckerBoardPlatformTimer == 0 && (gNetworkAreaTimer - o->oCheckerBoardPlatformTimer) >= loopLength) { if ((gNetworkAreaTimer - o->oCheckerBoardPlatformTimer) >= loopLength) {
o->oTimer = 0; u32 catchup = (gNetworkAreaTimer - o->oCheckerBoardPlatformTimer) / loopLength;
o->oCheckerBoardPlatformTimer = ((gNetworkAreaTimer - o->oCheckerBoardPlatformTimer) / loopLength) * loopLength; catchup *= loopLength;
} o->oCheckerBoardPlatformTimer += catchup;
} }
if (o->oDistanceToMario < 1000.0f) if (o->oDistanceToMario < 1000.0f)

View file

@ -10,18 +10,11 @@ void bhv_ddd_pole_init(void) {
void bhv_ddd_pole_update(void) { void bhv_ddd_pole_update(void) {
// make sure we're loaded and synchronized u32 loopLength = (((u16)o->oDDDPoleMaxOffset / 10) + 20) * 2;
if (!gNetworkAreaLoaded) { if ((gNetworkAreaTimer - o->oDDDPoleTimer) >= loopLength) {
o->oTimer = 0; u32 catchup = (gNetworkAreaTimer - o->oDDDPoleTimer) / loopLength;
o->oDDDPoleTimer = 0; catchup *= loopLength;
return; o->oDDDPoleTimer += catchup;
} else {
// catch up, jumping full loop cycles at a time
u16 loopTime = (((u16)o->oDDDPoleMaxOffset / 10) + 20) * 2;
if (o->oDDDPoleTimer == 0 && (gNetworkAreaTimer - o->oDDDPoleTimer) >= loopTime) {
o->oDDDPoleTimer = ((gNetworkAreaTimer - o->oDDDPoleTimer) / loopTime) * loopTime;
o->oTimer = 0;
}
} }
while (o->oDDDPoleTimer < gNetworkAreaTimer) { while (o->oDDDPoleTimer < gNetworkAreaTimer) {

View file

@ -31,6 +31,7 @@ struct NetworkSystem* gNetworkSystem = &gNetworkSystemSocket;
u16 networkLoadingLevel = 0; u16 networkLoadingLevel = 0;
bool gNetworkAreaLoaded = false; bool gNetworkAreaLoaded = false;
bool gNetworkAreaSyncing = true; bool gNetworkAreaSyncing = true;
u32 gNetworkAreaTimerClock = 0;
u32 gNetworkAreaTimer = 0; u32 gNetworkAreaTimer = 0;
struct StringLinkedList gRegisteredMods = { 0 }; struct StringLinkedList gRegisteredMods = { 0 };
@ -100,6 +101,7 @@ void network_on_init_area(void) {
gNetworkAreaLoaded = false; gNetworkAreaLoaded = false;
gNetworkAreaSyncing = true; gNetworkAreaSyncing = true;
gNetworkAreaTimer = 0; gNetworkAreaTimer = 0;
gNetworkAreaTimerClock = clock_elapsed_ticks();
} }
void network_on_loaded_area(void) { void network_on_loaded_area(void) {
@ -261,12 +263,10 @@ void network_update(void) {
networkLoadingLevel++; networkLoadingLevel++;
if (!gNetworkAreaLoaded && networkLoadingLevel >= LOADING_LEVEL_THRESHOLD) { if (!gNetworkAreaLoaded && networkLoadingLevel >= LOADING_LEVEL_THRESHOLD) {
gNetworkAreaLoaded = true; gNetworkAreaLoaded = true;
gNetworkAreaTimer = 0;
network_on_loaded_area(); network_on_loaded_area();
} }
} else if (gNetworkAreaLoaded) {
gNetworkAreaTimer++;
} }
gNetworkAreaTimer = (clock_elapsed_ticks() - gNetworkAreaTimerClock);
// send out update packets // send out update packets
if (gNetworkType != NT_NONE && network_player_any_connected()) { if (gNetworkType != NT_NONE && network_player_any_connected()) {

View file

@ -85,6 +85,7 @@ extern enum NetworkType gNetworkType;
extern bool gNetworkAreaLoaded; extern bool gNetworkAreaLoaded;
extern bool gNetworkAreaSyncing; extern bool gNetworkAreaSyncing;
extern u32 gNetworkAreaTimer; extern u32 gNetworkAreaTimer;
extern u32 gNetworkAreaTimerClock;
extern struct SyncObject gSyncObjects[]; extern struct SyncObject gSyncObjects[];
extern struct ServerSettings gServerSettings; extern struct ServerSettings gServerSettings;
extern struct StringLinkedList gRegisteredMods; extern struct StringLinkedList gRegisteredMods;

View file

@ -9,6 +9,7 @@
#include "object_constants.h" #include "object_constants.h"
#include "object_fields.h" #include "object_fields.h"
#include "model_ids.h" #include "model_ids.h"
#include "pc/utils/misc.h"
//#define DISABLE_MODULE_LOG 1 //#define DISABLE_MODULE_LOG 1
#include "pc/debuglog.h" #include "pc/debuglog.h"
@ -140,6 +141,7 @@ void network_receive_area(struct Packet* p) {
// read area variables // read area variables
packet_read(p, &gNetworkAreaTimer, sizeof(u32)); packet_read(p, &gNetworkAreaTimer, sizeof(u32));
gNetworkAreaTimerClock = clock_elapsed_ticks() - gNetworkAreaTimer;
// read removed sync ids // read removed sync ids
area_remove_sync_ids_clear(); area_remove_sync_ids_clear();

View file

@ -23,16 +23,27 @@ void update_all_mario_stars(void) {
} }
} }
f32 clock_elapsed(void) { static u64 clock_elapsed_ns(void) {
static bool sClockInitialized = false; static bool sClockInitialized = false;
static struct timespec clock_start; static u64 clock_start_ns;
if (!sClockInitialized) { if (!sClockInitialized) {
struct timespec clock_start;
clock_gettime(CLOCK_MONOTONIC, &clock_start); clock_gettime(CLOCK_MONOTONIC, &clock_start);
clock_start_ns = ((u64)clock_start.tv_sec) * 1000000000 + clock_start.tv_nsec;
sClockInitialized = true; sClockInitialized = true;
} }
struct timespec clock_current; struct timespec clock_current;
clock_gettime(CLOCK_MONOTONIC, &clock_current); clock_gettime(CLOCK_MONOTONIC, &clock_current);
return (clock_current.tv_sec - clock_start.tv_sec)
+ ((clock_current.tv_nsec - clock_start.tv_nsec) / 1000000000.0f); u64 clock_current_ns = ((u64)clock_current.tv_sec) * 1000000000 + clock_current.tv_nsec;
return (clock_current_ns - clock_start_ns);
}
f32 clock_elapsed(void) {
return (clock_elapsed_ns() / 1000000000.0f);
}
u32 clock_elapsed_ticks(void) {
return (clock_elapsed_ns() * 3 / 100000000);
} }

View file

@ -4,5 +4,6 @@
float smoothstep(float edge0, float edge1, float x); float smoothstep(float edge0, float edge1, float x);
void update_all_mario_stars(void); void update_all_mario_stars(void);
f32 clock_elapsed(void); f32 clock_elapsed(void);
u32 clock_elapsed_ticks(void);
#endif #endif