-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_entitystate.as
476 lines (371 loc) · 13.5 KB
/
game_entitystate.as
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* game_entitystate
by Outerbeast
Save and load states of various brush entities between maps eg breakables broken, doors opened, wall entities, pushables moved or triggers used
Entities that are meant to have their state saves will be marked with a special keyvalue.
Installation:-
- Place in scripts/maps
- Add
map_script game_entitystate
to your map cfg
OR
- Add
#include "game_entitystate"
to your main map script header
OR
- Create a trigger_script with these keys set in your map:
"classname" "trigger_script"
"m_iszScriptFile" "game_entitystate"
Usage:- There are two seperate entities that are used
game_entitystate_save: this entity will save the states of marked entities
game_entitystate_load: this entity will load the states of marked entities, if states were saved previously
Both entities will trigger "target" if their operation was successfull, and "message" if they failed
Entities that are meant to have their states saved and loaded must be marked with a custom key "$s_transition_name" with value thats unique to it.
The equivalent entity in the next map must also have the exact keyvalue as this one in order for the entities to be linked so that their state is kept.
Entity states supported:
Breakables - anything that can be broken, such as func_breakable, breakable doors or pushables, will remain broken
Pushables - func_pushable will have their current position saved
Doors - doors will save their open/closed state
Walls - func_wall/illusionary will save their toggled texture. func_wall_toggles instead will save their on/invisible state
Trams - func_(track)train entity will save their position and angles
Triggers - trigger_once will be removed if previously triggered.
Keys:-
"map" "mapname" - (game_entitystate_save only) Name of the map that entitystates will be saved to.
"targetname" "target_me" - If set, the entity will require manual triggering in order to perform its action. Otherwise, the entity should automatically operate when needed.
"target" "entity_targetname" - Triggers this when states are successfully saved/loaded
"message" "entity_targetname" - Triggers this when states have failed to save/load
*/
namespace GAME_ENTITYSTATE
{
enum enttype
{
NONE = 0,
BREAKABLES,
PUSHABLES,
DOORS,
WALLS,
TRAMS,
PLATS, // Not yet implemented
TRIGGERS
};
string strSavePath = "scripts/maps/store/", strSavedEnts;
array<EHandle> H_BREAKABLES, H_PUSHABLES, H_DOORS, H_WALLS, H_TRIGGERS, H_TRAMS;
bool blEntityRegistered = RegisterEntity();
bool RegisterEntity()
{
g_CustomEntityFuncs.RegisterCustomEntity( "GAME_ENTITYSTATE::CGameEntityState", "game_entitystate_save" );
g_CustomEntityFuncs.RegisterCustomEntity( "GAME_ENTITYSTATE::CGameEntityState", "game_entitystate_load" );
return g_CustomEntityFuncs.IsCustomEntity( "game_entitystate_save" ) && g_CustomEntityFuncs.IsCustomEntity( "game_entitystate_load" );
}
string TransitionName(EHandle hEntity)
{
if( !hEntity )
return "";
CustomKeyvalues@ kvEntity = hEntity.GetEntity().GetCustomKeyvalues();
if( kvEntity is null || !kvEntity.HasKeyvalue( "$s_transition_name" ) )
return "";
return kvEntity.GetKeyvalue( "$s_transition_name" ).GetString();
}
void GetTransitionEntities()
{
const Vector
vecWorldMins = Vector( -WORLD_BOUNDARY, -WORLD_BOUNDARY, -WORLD_BOUNDARY ),
vecWorldMaxs = Vector( WORLD_BOUNDARY, WORLD_BOUNDARY, WORLD_BOUNDARY );
array<CBaseEntity@> P_ENTITIES( g_EngineFuncs.NumberOfEntities() );
if( g_EntityFuncs.BrushEntsInBox( @P_ENTITIES, vecWorldMins, vecWorldMaxs ) < 1 )
return;
for( uint i = 0; i < P_ENTITIES.length(); i++ )
{
if( P_ENTITIES[i] is null || TransitionName( P_ENTITIES[i] ) == "" )
continue;
if( P_ENTITIES[i].GetClassname() == "func_breakable" && H_BREAKABLES.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_BREAKABLES.insertLast( P_ENTITIES[i] );
// Pushable
if( P_ENTITIES[i].IsBSPModel() && P_ENTITIES[i].pev.movetype == MOVETYPE_PUSHSTEP && H_PUSHABLES.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_PUSHABLES.insertLast( P_ENTITIES[i] );
if( cast<CBaseDoor@>( P_ENTITIES[i] ) !is null && H_DOORS.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_DOORS.insertLast( P_ENTITIES[i] );
if( ( P_ENTITIES[i].GetClassname().StartsWith( "func_wall" ) || P_ENTITIES[i].GetClassname() == "func_illusionary" ) && H_WALLS.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_WALLS.insertLast( P_ENTITIES[i] );
if( P_ENTITIES[i].GetClassname().EndsWith( "train" ) && H_TRAMS.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_TRAMS.insertLast( P_ENTITIES[i] );
if( P_ENTITIES[i].GetClassname() == "trigger_once" && H_TRIGGERS.findByRef( EHandle( P_ENTITIES[i] ) ) < 0 )
H_TRIGGERS.insertLast( P_ENTITIES[i] );
}
}
// !-TO-DO-!: save "health" for anything that is breakable
int FetchEntityStates(const int iEntType)
{
array<EHandle> H_ENTITIES;
switch( iEntType )
{
case BREAKABLES:
H_ENTITIES = H_BREAKABLES;
break;
case PUSHABLES:
H_ENTITIES = H_PUSHABLES;
break;
case DOORS:
H_ENTITIES = H_DOORS;
break;
case WALLS:
H_ENTITIES = H_WALLS;
break;
case TRIGGERS:
H_ENTITIES = H_TRIGGERS;
break;
default: return 0;
}
if( H_ENTITIES.length() < 1 )
return 0;
int iEntitiesProcessed = 0;
for( uint i = 0; i < H_ENTITIES.length(); i++ )
{
if( !H_ENTITIES[i] || TransitionName( H_ENTITIES[i] ) == "" )
continue;
CBaseEntity@ pEntity = H_ENTITIES[i].GetEntity();
switch( iEntType )
{
case PUSHABLES:
case TRAMS: // Tram entities untested, but the logic is sound
{
strSavedEnts += ";" + TransitionName( pEntity ) + ";" + pEntity.pev.origin.ToString().Replace( ", ", ";" );
if( iEntType == TRAMS )
strSavedEnts += ";" + TransitionName( pEntity ) + ";" + pEntity.pev.angles.ToString().Replace( ", ", ";" );
iEntitiesProcessed++;
break;
}
case DOORS:
{
strSavedEnts += ";" + TransitionName( pEntity );
if( cast<CBaseDoor@>( pEntity ) !is null )
{
strSavedEnts += ";" + cast<CBaseDoor@>( pEntity ).GetToggleState();
iEntitiesProcessed++;
}
break;
}
case WALLS:
{
if( pEntity.GetClassname() == "func_wall" || pEntity.GetClassname() == "func_illusionary" )
strSavedEnts += ";" + TransitionName( pEntity ) + ";" + pEntity.pev.frame;
else if( pEntity.GetClassname() == "func_wall_toggle" )
strSavedEnts += ";" + TransitionName( pEntity ) + ";" + ( pEntity.pev.solid != SOLID_NOT );
iEntitiesProcessed++;
break;
}
default:
{
strSavedEnts += ";" + TransitionName( pEntity );
iEntitiesProcessed++;
break;
}
}
}
return strSavedEnts != "" ? iEntitiesProcessed : 0;
}
int ApplyEntityStates(const int iEntType)
{
if( strSavedEnts == "" )
return 0;
const array<string> STR_SAVED_ENTS = strSavedEnts.Split( ";" );
if( STR_SAVED_ENTS.length() < 1 )
return 0;
array<EHandle> H_ENTITIES;
switch( iEntType )
{
case BREAKABLES:
H_ENTITIES = H_BREAKABLES;
break;
case PUSHABLES:
H_ENTITIES = H_PUSHABLES;
break;
case DOORS:
H_ENTITIES = H_DOORS;
break;
case WALLS:
H_ENTITIES = H_WALLS;
break;
case TRIGGERS:
H_ENTITIES = H_TRIGGERS;
break;
default: return 0;
}
if( H_ENTITIES.length() < 1 )
return 0;
int iEntitiesProcessed = 0;
for( uint i = 0; i < H_ENTITIES.length(); i++ )
{
if( !H_ENTITIES[i] || TransitionName( H_ENTITIES[i] ) == "" )
continue;
CBaseEntity@ pEntity = H_ENTITIES[i].GetEntity();
if( STR_SAVED_ENTS.find( TransitionName( pEntity ) ) < 0 )
{
g_EntityFuncs.Remove( pEntity );
continue;
}
switch( iEntType )
{
case PUSHABLES:
case TRAMS:
{
Vector vecSavedPos;
vecSavedPos.x = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 1] );
vecSavedPos.y = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 2] );
vecSavedPos.z = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 3] );
g_EntityFuncs.SetOrigin( pEntity, vecSavedPos );
if( iEntType == TRAMS )
{
Vector vecSavedAngles;
vecSavedAngles.x = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 4] );
vecSavedAngles.y = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 5] );
vecSavedAngles.z = atof( STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 6] );
}
iEntitiesProcessed++;
break;
}
case DOORS:
{
CBaseDoor@ pDoor = cast<CBaseDoor@>( pEntity );
if( pDoor is null )
break;
const string state = STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pDoor ) ) + 1];
pDoor.SetToggleState( TOGGLE_STATE( atoi( state ) ) );
iEntitiesProcessed++;
break;
}
case WALLS:
{
const string state = STR_SAVED_ENTS[STR_SAVED_ENTS.find( TransitionName( pEntity ) ) + 1];
if( pEntity.GetClassname() == "func_wall" || pEntity.GetClassname() == "func_illusionary" )
pEntity.pev.frame = atof( state );
else if( pEntity.GetClassname() == "func_wall_toggle" )
pEntity.Use( pEntity, pEntity, USE_TYPE( atoi( state ) ), 0.0f );
iEntitiesProcessed++;
break;
}
default:
iEntitiesProcessed++;
break;
}
}
return iEntitiesProcessed;
}
bool Save(string strMap)
{
g_FileSystem.RemoveFile( strSavePath + strMap + ".dat" );
if( strMap == "" || !g_EngineFuncs.IsMapValid( strMap ) )
{
//g_EngineFuncs.ServerPrint( "!-----------------NEXTMAP INVALID!------------------!\n" );
return false;
}
GetTransitionEntities();
int iStatesSaved = 0;
for( int i = BREAKABLES; i <= TRIGGERS; i++ )
iStatesSaved += FetchEntityStates( i );
if( iStatesSaved < 1 )
{
//g_EngineFuncs.ServerPrint( "!-----------------ENTITY STATES FETCH FAILED!------------------!\n" );
return false;
}
File@ fileSave = g_FileSystem.OpenFile( strSavePath + strMap + ".dat", OpenFile::WRITE );
if( fileSave is null || !fileSave.IsOpen() )
{
//g_EngineFuncs.ServerPrint( "!-----------------WRITE FAILED!------------------!\n" );
return false;
}
fileSave.Write( strSavedEnts );
fileSave.Close();
//g_EngineFuncs.ServerPrint( "!-----------------SAVING ENTITY STATES: " + strSavedEnts + " ------------------!\n" );
return strSavedEnts != "";
}
bool Load(string strPrevMap)
{
if( strPrevMap == "" )
return false;
File@ fileLoad = g_FileSystem.OpenFile( strSavePath + strPrevMap + ".dat", OpenFile::READ );
if( fileLoad is null || !fileLoad.IsOpen() )
{
//g_EngineFuncs.ServerPrint( "!-----------------READ FAILED!------------------!\n" );
return false;
}
fileLoad.ReadLine( strSavedEnts );
fileLoad.Close();
GetTransitionEntities();
//g_EngineFuncs.ServerPrint( "!-----------------LOADING ENTITY STATES: " + strSavedEnts + " ------------------!\n" );
int iStatesLoaded = 0;
for( int i = BREAKABLES; i <= TRIGGERS; i++ )
iStatesLoaded += ApplyEntityStates( i );
return iStatesLoaded > 0;
}
void Delete(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
const string strPrevMap = pCaller.pev.netname;
if( strPrevMap == "" || !g_EngineFuncs.IsMapValid( strPrevMap ) )
return;
g_FileSystem.RemoveFile( strSavePath + strPrevMap + ".dat" );
}
// !-TEMPORARY-!: Cleanup routine for savestates - remove save files when these maps are NOT running
void Cleanup(const string strSaveMapList)
{
if( strSaveMapList == "" )
return;
array<string> STR_SAVE_MAPS = strSaveMapList.Split( ";" );
if( STR_SAVE_MAPS.find( g_Engine.mapname ) >= 0 )
return;
for( uint i = 0; i < STR_SAVE_MAPS.length(); i++ )
g_FileSystem.RemoveFile( strSavePath + STR_SAVE_MAPS[i] + ".dat" );
}
// !-TO-DO-!: logic for automatically deleting current level's loaded state when level is changing
final class CGameEntityState : ScriptBaseEntity
{
private string strMap
{
get { return string( pev.netname ); }
set { pev.netname = string( value ); }
};
bool KeyValue(const string& in szKey, const string& in szValue)
{
if( szKey == "map" )
strMap = szValue;
else
return BaseClass.KeyValue( szKey, szValue );
return true;
}
void Spawn()
{
self.pev.solid = SOLID_NOT;
self.pev.movetype = MOVETYPE_NONE;
self.pev.effects |= EF_NODRAW;
g_EntityFuncs.SetOrigin( self, self.pev.origin );
// Delayed a millisecond later because of Sod's Law.
g_Scheduler.SetTimeout( this, "Init", 0.01f );
BaseClass.Spawn();
}
void Init()
{
if( self.GetTargetname() == "" )
{
if( self.GetClassname().EndsWith( "_save" ) )
g_Hooks.RegisterHook( Hooks::Game::MapChange, MapChangeHook( this.MapChange ) );
else if( self.GetClassname().EndsWith( "_load" ) )
self.Use( self, self, USE_TOGGLE, 0.1f );
}
}
void Use(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
if( ( self.GetClassname().EndsWith( "_save" ) && Save( strMap ) ) ||
( self.GetClassname().EndsWith( "_load" ) && Load( g_Engine.mapname ) ) )
self.SUB_UseTargets( pActivator, USE_TOGGLE, 0.0f );
else
g_EntityFuncs.FireTargets( self.pev.message, pActivator, self, USE_TOGGLE, 0.0f, 0.0f );
}
HookReturnCode MapChange()
{
self.Use( self, self, USE_ON, 0.0f );
return HOOK_CONTINUE;
}
};
}
/* Special thanks to:-
- _RC, for original concept and design
*/