aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/ai_saverestore.cpp
blob: 917a73b264d2f0cfa23e7824c02f726be89db5f9 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"

#include "isaverestore.h"
#include "saverestore_utlvector.h"
#include "ai_saverestore.h"
#include "ai_basenpc.h"
#include "ai_squad.h"
#include "ai_network.h"
#include "ai_networkmanager.h"

#ifdef HL2_DLL
#include "npc_playercompanion.h"
#endif // HL2_DLL

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

static short AI_SAVE_RESTORE_VERSION = 2;

//-----------------------------------------------------------------------------

class CAI_SaveRestoreBlockHandler : public CDefSaveRestoreBlockHandler
{
public:
	const char *GetBlockName()
	{
		return "AI";
	}

	//---------------------------------

	void Save( ISave *pSave )
	{
		pSave->StartBlock( "Squads" );
		short nSquads = (short)g_AI_SquadManager.NumSquads();
		pSave->WriteShort( &nSquads );
		
		AISquadsIter_t iter;
		string_t squadName;
		CAI_Squad* pSquad = g_AI_SquadManager.GetFirstSquad( &iter );
		while (pSquad)
		{
			squadName = MAKE_STRING( pSquad->GetName() );
			pSave->WriteString( "", &squadName ); // Strings require a header to be read properly
			pSave->WriteAll( pSquad );
			pSquad = g_AI_SquadManager.GetNextSquad( &iter );
		}
		
		pSave->EndBlock();

		//---------------------------------
		
		pSave->StartBlock( "Enemies" );
		short nMemories = 0;
		
		CAI_BaseNPC **ppAIs = g_AI_Manager.AccessAIs();
		int i;

		for ( i = 0; i < g_AI_Manager.NumAIs(); i++ )
		{
			if ( ppAIs[i]->GetEnemies() )
				nMemories++;
		}

		pSave->WriteShort( &nMemories );
		
		for ( i = 0; i < g_AI_Manager.NumAIs(); i++ )
		{
			if ( ppAIs[i]->GetEnemies() )
			{
				CBaseEntity *p = ppAIs[i];
				pSave->WriteEntityPtr( &p );
				pSave->WriteAll( ppAIs[i]->GetEnemies() );
			}
		}
		pSave->EndBlock();
	}

	//---------------------------------

	void WriteSaveHeaders( ISave *pSave )
	{
		pSave->WriteShort( &AI_SAVE_RESTORE_VERSION );
	}
	
	//---------------------------------

	void ReadRestoreHeaders( IRestore *pRestore )
	{
		// No reason why any future version shouldn't try to retain backward compatability. The default here is to not do so.
		short version;
		pRestore->ReadShort( &version );
		m_fDoLoad = ( version == AI_SAVE_RESTORE_VERSION );
	}

	//---------------------------------

	void Restore( IRestore *pRestore, bool createPlayers )
	{
		// Initialize the squads (as there's no spawn)
		CAI_BaseNPC **ppAIs = g_AI_Manager.AccessAIs();
		int i;

		for ( i = 0; i < g_AI_Manager.NumAIs(); i++ )
		{
			ppAIs[i]->InitSquad();
		}
		
		if ( m_fDoLoad )
		{
			pRestore->StartBlock();
			// Fixup all the squads
			CAI_Squad  ignored;
			CAI_Squad *pSquad;
			string_t   squadName;
			int 	   nSavedSquads = pRestore->ReadShort();
			
			while ( nSavedSquads-- )
			{
				int sizeData = pRestore->SkipHeader();
				pRestore->ReadString( &squadName, 1, sizeData );
				pSquad = g_AI_SquadManager.FindSquad( squadName );
				if ( !pSquad )
					pSquad = &ignored; // if all of the AIs in a squad failed to spawn, there would be no squad
				pRestore->ReadAll( pSquad );
			}
			pRestore->EndBlock();
			
			//---------------------------------
			// Now load memories for unsquadded npcs
			
			pRestore->StartBlock();
			CAI_Enemies ignoredMem;
			short nMemories = pRestore->ReadShort();
			
			CBaseEntity *pAI; 
			
			while ( nMemories-- )
			{
				pRestore->ReadEntityPtr( &pAI );
				
				if ( pAI )
					pRestore->ReadAll( ((CAI_BaseNPC *)pAI)->GetEnemies() );
				else
					pRestore->ReadAll( &ignoredMem ); // AI probably failed to spawn
			}
			
			pRestore->EndBlock();
		}
		
		if ( g_AI_Manager.NumAIs() && g_pBigAINet->NumNodes() == 0 && !g_pAINetworkManager->NetworksLoaded() )
		{
			Msg( "***\n");
			Msg( "ERROR: Loaded save game with no node graph. Load map and build node graph first!\n");
			Msg( "***\n");
			CAI_BaseNPC::m_nDebugBits |= bits_debugDisableAI;
			g_pAINetworkManager->MarkDontSaveGraph();
		}
	}

	void PostRestore( void )
	{
#ifdef HL2_DLL
		// We need this list to be regenerated
		OverrideMoveCache_ForceRepopulateList();
#endif // HL2_DLL
	}

private:
	bool m_fDoLoad;
};

//-----------------------------------------------------------------------------

CAI_SaveRestoreBlockHandler g_AI_SaveRestoreBlockHandler;

//-------------------------------------

ISaveRestoreBlockHandler *GetAISaveRestoreBlockHandler()
{
	return &g_AI_SaveRestoreBlockHandler;
}

//=============================================================================