-
Notifications
You must be signed in to change notification settings - Fork 0
/
psprite-speed-scaler.zc
119 lines (103 loc) · 2.71 KB
/
psprite-speed-scaler.zc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class PSpriteSpeedScaler play {
PSprite ps;
PSpriteSpeedScaler next;
void SkipTic() {
if (ps.Tics < 1)
// Don't act on indefinite states.
;
else if (ps.Tics > 1)
ps.Tics--;
else
ps.SetState(ps.CurState.NextState);
}
void DelayTic() {
if (ps.Tics > 0)
ps.Tics++;
}
}
struct PSpriteSpeedScalerSet play {
PSpriteSpeedScaler first;
private float debt;
void Clear() {
for (let sci = first; sci; sci = sci.next)
if (sci.ps)
sci.ps.bPowDouble = true;
first = null;
debt = 0;
}
void MoveAll(bool skipOrDelay, Inventory item = null) {
for (let ps = item.Owner.player.psprites; ps; ps = ps.Next)
if (ps && (!item || ps.Caller == item)) {
PSpriteSpeedScaler scl = null;
for (PSpriteSpeedScaler sci = first, psci = null; sci; psci = sci, sci = sci.next) {
if (!sci.ps || sci.ps.bDestroyed) {
if (psci)
psci.next = sci.next;
else
first = sci.next;
}
else if (sci.ps == ps) {
scl = sci;
break;
}
}
if (!scl) {
scl = new("PSpriteSpeedScaler");
scl.ps = ps;
scl.next = first;
ps.bPowDouble = false;
first = scl;
}
if (skipOrDelay)
scl.SkipTic();
else
scl.DelayTic();
}
}
void Tick(Inventory item, float scale) {
if (debt >= 1.) {
// Waiting for a debt. Delay all scalers, but do nothing else.
if (debt >= 2.)
MoveAll(false, item);
debt -= 1.;
}
else {
// Time to apply the scaling thing again.
debt += scale;
// Apply PowerDoubleFiringSpeed, if appropriate.
if (
item && item.Owner && (item is "Weapon") &&
item.Owner.FindInventory("PowerDoubleFiringSpeed", true)
)
debt -= 1.;
if (debt >= 1.) {
MoveAll(false, item);
// Don't modify debt here. Wait for the next tic before doing that. Otherwise, any delay would cause us to endlessly wait on the same state!
}
else while (debt <= -1.) {
// Skip tics as needed.
MoveAll(true, item);
debt += 1.;
}
}
// Occasionally look for and remove unused scalers.
// This can happen when the scale is so low that a flash animation is skipped entirely.
// This has approximately O(n^2) complexity, so don't do it all the time.
if (!(gametic & 127)) {
for (PSpriteSpeedScaler sci = first, psci = null; sci; psci = sci, sci = sci.next) {
bool used = false;
for (let ps = item.Owner.player.psprites; ps; ps = ps.Next)
if (ps.Caller == item && sci.ps == ps) {
used = true;
break;
}
if (!used || sci.ps.bDestroyed) {
if (psci)
psci.next = sci.next;
else
first = sci.next;
}
}
}
}
}