AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxLoadProgress.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: loading percentage for map loads. Pure, unit tested in tests/cpp/test_load_progress.cpp.
3 *
4 * A map change runs in phases. Two have counters from the engine (static geometry, entities); the
5 * others (script compile, setup, the new map's start scripts) do not, so their progress is the time
6 * spent against what that phase took the last time this map loaded (or a default on a first
7 * visit). Phase shares come from those times too. The shown value never goes backwards and stays
8 * below 100 until the load is done.
9 */
10#ifndef LUX_LOAD_PROGRESS_H
11#define LUX_LOAD_PROGRESS_H
12
14{
15 eLuxLoadPhase_Prepare, // save the old map, compile the new map's script, read the map file
16 eLuxLoadPhase_Static, // static geometry and physics (map cache) [counter]
17 eLuxLoadPhase_Entities, // props, enemies, lights, sounds, areas [counter]
18 eLuxLoadPhase_Setup, // after the world load: entity setup, AI nodes
19 eLuxLoadPhase_Enter, // destroy the old map, saved state, OnStart / OnEnter scripts
21};
22
23/** Typical times (ms) on a first visit, from measured cached loads. */
24static const float kLuxLoadPhaseDefaultMs[eLuxLoadPhase_LastEnum] = { 300, 900, 1300, 200, 2200 };
25
27{
28public:
30
31 /** Start a load; avExpectedMs = each phase's time from the last load of this map (or NULL). */
32 void Begin(const float* avExpectedMs, unsigned long alNowMs)
33 {
34 float fTotal = 0;
35 for(int i = 0; i < eLuxLoadPhase_LastEnum; ++i)
36 {
37 mvExpected[i] = (avExpectedMs && avExpectedMs[i] > 0) ? avExpectedMs[i] : kLuxLoadPhaseDefaultMs[i];
38 if(mvExpected[i] < 50) mvExpected[i] = 50;
39 fTotal += mvExpected[i];
40 mvTaken[i] = 0;
41 }
42 float fStart = 0;
43 for(int i = 0; i < eLuxLoadPhase_LastEnum; ++i)
44 {
45 mvStart[i] = fStart;
46 mvShare[i] = mvExpected[i] / fTotal;
47 fStart += mvShare[i];
48 }
49 mlPhase = eLuxLoadPhase_Prepare;
50 mlPhaseStartMs = alNowMs;
51 mfDone = 0; mfTotal = 0;
52 mfShown = 0;
53 mbActive = true;
54 }
55
56 /** Move to a later phase (earlier phases count as complete). */
57 void SetPhase(int alPhase, unsigned long alNowMs)
58 {
59 if(alPhase <= mlPhase || alPhase >= eLuxLoadPhase_LastEnum) return;
60 mvTaken[mlPhase] += (float)(alNowMs - mlPhaseStartMs);
61 mlPhase = alPhase;
62 mlPhaseStartMs = alNowMs;
63 mfDone = 0; mfTotal = 0;
64 }
65
66 /** Counter of the current phase (static objects, entities). */
67 void SetCount(int alDone, int alTotal) { mfDone = (float)alDone; mfTotal = (float)alTotal; }
68
69 /** 0..99 while loading. */
70 int Percent(unsigned long alNowMs)
71 {
72 if(mbActive == false) return 100;
73 float fIn;
74 if(mfTotal > 0) fIn = mfDone / mfTotal;
75 else
76 {
77 // No counter: time against the expected time, easing toward (never reaching) the end.
78 float t = (float)(alNowMs - mlPhaseStartMs) / mvExpected[mlPhase];
79 fIn = t < 1 ? t * 0.9f : 0.9f + 0.1f * (1.0f - 1.0f / (1.0f + (t - 1.0f)));
80 }
81 if(fIn < 0) fIn = 0;
82 if(fIn > 1) fIn = 1;
83 float f = (mvStart[mlPhase] + mvShare[mlPhase] * fIn) * 100.0f;
84 if(f > 99) f = 99;
85 if(f > mfShown) mfShown = f;
86 return (int)mfShown;
87 }
88
89 /** The load is done; avTakenMs receives each phase's time, to expect next time. */
90 void Finish(unsigned long alNowMs, float* avTakenMs)
91 {
92 mvTaken[mlPhase] += (float)(alNowMs - mlPhaseStartMs);
93 if(avTakenMs) for(int i = 0; i < eLuxLoadPhase_LastEnum; ++i) avTakenMs[i] = mvTaken[i];
94 mbActive = false;
95 }
96
97 bool IsActive() const { return mbActive; }
98 int GetPhase() const { return mlPhase; }
99
100private:
101 float mvExpected[eLuxLoadPhase_LastEnum];
102 float mvShare[eLuxLoadPhase_LastEnum];
103 float mvStart[eLuxLoadPhase_LastEnum];
104 float mvTaken[eLuxLoadPhase_LastEnum];
105 int mlPhase;
106 unsigned long mlPhaseStartMs;
107 float mfDone, mfTotal;
108 float mfShown;
109 bool mbActive;
110};
111
112#endif
eLuxLoadPhase
@ eLuxLoadPhase_Enter
@ eLuxLoadPhase_LastEnum
@ eLuxLoadPhase_Setup
@ eLuxLoadPhase_Prepare
@ eLuxLoadPhase_Static
@ eLuxLoadPhase_Entities
static const float kLuxLoadPhaseDefaultMs[eLuxLoadPhase_LastEnum]
Typical times (ms) on a first visit, from measured cached loads.
bool IsActive() const
void Finish(unsigned long alNowMs, float *avTakenMs)
The load is done; avTakenMs receives each phase's time, to expect next time.
void Begin(const float *avExpectedMs, unsigned long alNowMs)
Start a load; avExpectedMs = each phase's time from the last load of this map (or NULL).
void SetCount(int alDone, int alTotal)
Counter of the current phase (static objects, entities).
int Percent(unsigned long alNowMs)
0..99 while loading.
int GetPhase() const
void SetPhase(int alPhase, unsigned long alNowMs)
Move to a later phase (earlier phases count as complete).