-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZScript.zsc
247 lines (174 loc) · 9.38 KB
/
ZScript.zsc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
version "2.4.0"
#include "ZScript/AutoAmbientCore/Constants.zsc"
#include "ZScript/AutoAmbientCore/Utils.zsc"
#include "ZScript/AutoAmbientCore/DListTexCache.zsc"
#include "ZScript/AutoAmbientCore/SListSpawned.zsc"
#include "ZScript/AutoAmbientCore/DataClasses.zsc"
#include "ZScript/AutoAmbientCore/AutoActors.zsc"
#include "ZScript/AutoAmbientCore/ConfigParser.zsc"
#include "ZScript/AutoAmbientCore/SpawnActors.zsc"
class AutoAmbientGlobalHandler: StaticEventHandler {
const AutoAmbientHandlerOrder = 0x7FAA0004; // ZChecker: 0x7FAA0005 and 0x7FAA0006.
const replicateParserMaxAmount = 24;
int replicateParserAmount; // To prevent infinite recursive including loop.
//int currentGlobalLumpPos; // Dangerous variable, in fact.
int parserErrorLine;
String parserErrorFilename;
bool dontLoadFlag;
// Warning! StaticEventHandler nullifies all its fields on heaviest
//game mode changes (e. g., from VM loading to title screen).
AutoAmbientLogger logger;
AutoAmbientSoundActorConfiguration autoclassConfig;
//int actionsTimeCounter; // "Object::MSTime();"
AutoAmbientMapSet maptextures;
Array<AutoAmbientTexture> textures;
Array<AutoAmbientGroup> groups;
Array<AutoAmbientTextureParameter> textureparams;
Array<AutoAmbientSoundDefinition> sounddefs;
double topLevelBound, rightLevelBound, bottomLevelBound, leftLevelBound;
// Configuration init and file parsing:
override void OnRegister() {
SetOrder( AutoAmbientHandlerOrder );
logger = AutoAmbientLogger.Get();
if ( !IsAutoAmbientEnabled() ) {
logger.Log( LL_Detailed, GetClassName() .. "::OnRegister(). AutoAmbient isn't enabled, parsing skipped." );
dontLoadFlag = true;
Super.OnRegister();
return;
}
maptextures = AutoAmbientMapSet.Create();
textures.Clear();
groups.Clear();
textureparams.Clear();
sounddefs.Clear();
parserErrorLine = AAParser_NoErrorLine;
//ResetGlobalLumpPos();
autoclassConfig = new( 'AutoAmbientSoundActorConfiguration' );
AutoAmbientConfigParser.Create( self ).ParseFile( "aambient", true, -1 );
if ( parserErrorLine == AAParser_NoErrorLine ) {
logger.Log( LL_Main, TEXTCOLOR_GRAY .. GetClassName() .. "::OnRegister(). \c-Loading configuration done." );
AutoAmbientTexture invalidTexture = FindInvalidTexture();
if ( invalidTexture ) {
logger.Log( LL_Emergency, GetClassName() .. "::OnRegister(). Texture definition ID " .. invalidTexture.id .. " is not valid." );
logger.Log( LL_Main, GetClassName() .. "::OnRegister(). Validation callback: "
.. "id=" .. TexMan.GetName( invalidTexture.id ) .. ", size.Length()=" .. invalidTexture.size.Length()
.. ", groups.Size()=" .. invalidTexture.groups.Size() .. ", groupparams.Size()=" .. invalidTexture.groupparams.Size()
.. ", addingdontLoadFlag=" .. invalidTexture.invalidFlag );
dontLoadFlag = true;
} else if ( !groups.Size() ) {
logger.Log( LL_Main, GetClassName() .. "::OnRegister(). No group blocks provided in the configuration file." );
dontLoadFlag = true;
} else if ( !textures.Size() ) {
logger.Log( LL_Main, GetClassName() .. "::OnRegister(). No texture definition blocks provided in the configuration file." );
dontLoadFlag = true;
}
} else {
dontLoadFlag = true;
logger.Log( LL_Emergency, "Error during configuration files parsing in \"" .. parserErrorFilename .. "\" on line " .. parserErrorLine .. "." );
logger.Log( LL_Emergency, String.Format( TEXTCOLOR_GRAY .. GetClassName() .. "::OnRegister(). EGameType 0x%03x, EGameAction %i, multiplayer %i, dm %i.",
gameinfo.gametype, gameaction, multiplayer, deathmatch ) );
}
if ( !dontLoadFlag ) {
CVar optionCVar = CVar.FindCVar( "mcm_aambient_showparseddata" );
// See "Utils.zsc" -> "extend class AutoAmbientGlobalHandler".
if ( optionCVar && optionCVar.GetBool() && logger.aaCurLogLevel >= LL_Detailed )
OnRegisterDebugFullOutput();
}
logger = NULL; // See note right before variable definition.
Super.OnRegister();
} // of override void OnRegister() {}
override void WorldLoaded( WorldEvent e ) {
// Skip all other checks if game is loaded from save,
//if an error occured, etc.
if ( e.IsSaveGame || dontLoadFlag || !IsAutoAmbientEnabled() ) {
Super.WorldLoaded( e );
return;
}
logger = AutoAmbientLogger.Get();
// Static event handlers saves information through levels.
for ( int i = 0; i < groups.Size(); i++ )
groups[ i ].ClearSpawned();
// Locating a top/right/bottom/left level border:
LocateLevelBorders();
logger.Log( LL_Detailed, "Level bound box: top " .. topLevelBound .. ", right " .. rightLevelBound .. ", bottom " .. bottomLevelBound .. ", left " .. leftLevelBound .. "." );
// Spawning itself (see "ZScript/AutoAmbientCore/SpawnActors.zsc"):
AutoAmbientCacheTextureKeeper cachedTextures = AutoAmbientCacheTextureKeeper.CreateKeeper( self );
int totalAutoAmbientActorsSpawned = SpawnAllActors( cachedTextures );
// Partial clearing actor arrays for fitting to the limit (we cannot
//know in advance how many actors will be spawned on level):
int reservedSoundChannels = 32;
CVar optionCVar = CVar.GetCVar( "mcm_aambient_sndchannelsreserve" );
if ( optionCVar )
reservedSoundChannels = optionCVar.GetInt();
else
logger.Log( LL_Main, logger.LLCOLOR_EMERGENCY .. "Cannot find CVar \"mcm_aambient_sndchannelsreserve\", using default value (32)." );
int availChannels = snd_channels - reservedSoundChannels;
if ( availChannels < 24 ) {
logger.Log( LL_Detailed, "Calculated available sound channels amount is too little (" .. TEXTCOLOR_YELLOW .. availChannels .. "\c-), set to 24." );
availChannels = 24;
}
// Cycle isn't the best choice, of course...
while ( totalAutoAmbientActorsSpawned > availChannels ) {
logger.Log( LL_Detailed, "Partially clearing from " .. TEXTCOLOR_YELLOW .. totalAutoAmbientActorsSpawned .. "\c- sound actors because of limit of " .. TEXTCOLOR_FIRE .. availChannels .. "\c- available sound channels (reserved " .. reservedSoundChannels .. ")." );
int newTotalActorsSpawned = 0;
for ( int i = 0; i < groups.Size(); i++ ) {
int prevActorsAmount = groups[ i ].spawned.Size();
if ( prevActorsAmount > 0 ) {
groups[ i ].spawned.PartiallyClear( 1.0 - 0.85 * availChannels / double( totalAutoAmbientActorsSpawned ) );
int newActorsAmount = groups[ i ].spawned.Size();
newTotalActorsSpawned += newActorsAmount;
logger.Log( LL_Debug, "Group " .. groups[ i ].name .. ": was " .. prevActorsAmount .. " actors, became " .. newActorsAmount .. " actors." );
}
}
totalAutoAmbientActorsSpawned = newTotalActorsSpawned;
} // of while ( totalAutoAmbientActorsSpawned > availChannels ) {}
// Post-logging:
logger.Log( LL_Detailed, GetClassName() .. "::WorldLoaded(). Total ambient actors spawned: " .. totalAutoAmbientActorsSpawned .. "." );
if ( logger.aaCurLogLevel >= LL_Debug ) {
logger.Log( LL_Debug, "AutoAmbientCacheTextureKeeper. Head " .. cachedTextures.head .. " <<->> tail " .. cachedTextures.tail .. "." );
AutoAmbientCacheTexture debugTexCache = cachedTextures.head;
uint debugTotalTexCacheAmount = 0;
while ( debugTexCache ) {
String debugTexCacheName = String.Format( "%8s", TexMan.GetName( debugTexCache.texID ) );
logger.Log( LL_Debug, "|- Texture " .. debugTexCacheName .. " freq " .. debugTexCache.frequency .. ": (" .. debugTexCache.prev .. ")<p " .. debugTexCache .. " n>(" .. debugTexCache.next .. ")" );
debugTexCache = debugTexCache.next;
debugTotalTexCacheAmount++;
}
logger.Log( LL_Debug, "AutoAmbientCacheTextureKeeper. Total texture cache nodes amount: " .. debugTotalTexCacheAmount .. "." );
for ( int i = 0; i < groups.Size(); i++ ) {
uint spawnedInGroup = groups[ i ].spawned.Size();
if ( !spawnedInGroup )
logger.Log( LL_Debug, GetClassName() .. "::WorldLoaded(). No spawned actors for group \"" .. groups[ i ].name .. "\"." );
else
logger.Log( LL_Debug, GetClassName() .. "::WorldLoaded(). Group \"" .. TEXTCOLOR_BLUE .. groups[ i ].name .. logger.LLCOLOR_DEBUG .. "\" total spawned: " .. TEXTCOLOR_GRAY .. spawnedInGroup .. logger.LLCOLOR_Debug .. "." );
if ( groups[ i ].GridCreated() )
groups[ i ].PrintGrid( logger );
}
}
optionCVar = CVar.GetCVar( "mcm_aambient_showactorsatstart" );
if ( optionCVar && optionCVar.GetBool() )
EventHandler.SendNetworkEvent( "mcm_autoambient_actors_visibility", 1 );
// Yes, I know perfectly well that garbage collector will successfully
//destroy an array.
cachedTextures.Clear();
Super.WorldLoaded( e );
} // of override void WorldLoaded( WorldEvent e ) {}
override void NetworkProcess( ConsoleEvent e ) {
if ( e.name == "mcm_autoambient_actors_visibility" ) {
bool setInvisibleFlag = ( e.args[ 0 ] == 0 );
for ( int i = 0; i < groups.Size(); i++ ) {
AutoAmbientListSpawnedKeeper spawnedKeeper = groups[ i ].spawned;
Actor curspawned = spawnedKeeper.ActorIteratorBegin();
while ( curspawned ) {
curspawned.bINVISIBLE = setInvisibleFlag;
curspawned = spawnedKeeper.ActorIteratorNext();
}
} // of for ( int i = 0; i < groups.Size(); i++ ) {}
AutoAmbientLogger.Get().Log( LL_Detailed, "Changed automatic actors visibility to " .. !setInvisibleFlag );
} else if ( e.name == "mcm_autoambient_destroy_actors" ) {
for ( int i = 0; i < groups.Size(); i++ )
groups[ i ].spawned.Clear();
}
Super.NetworkProcess( e );
} // of override void NetworkProcess( ConsoleEvent e ) {}
} // of class AmbientHandler: EventHandler {}