forked from 1dot13/source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPXmlTeams.cpp
251 lines (201 loc) · 5.55 KB
/
MPXmlTeams.cpp
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
#include "MPXmlTeams.hpp"
#include "sgp.h"
#include "FileMan.h"
#include "expat.h"
#include "XML.h"
#include "random.h"
#include <cstdio>
MultiplayerTeams mpTeams = MultiplayerTeams();
// fallback data
const int MultiplayerTeams::random_merc_teams[NUM_RANDOM_MERC_TEAMS][RANDOM_MERC_TEAM_SIZE] = {
{ 16, 10, 19, 25, 4 , 11, 39 }, // Gus , Shadow, Spider , Raven , Vicki , Red , Meltdown
{ 29, 36, 28, 2 , 22, 8 , 32 }, // Magic , Scope, Danny , Lynx , Hitman , Steroid , Malice
{ 12, 5, 20, 23, 48, 34, 17 }, // Reaper , Trevor, Cliff , Buzz , Cougar , Nails , Buns
{ 31, 7, 33, 35, 27, 37, 1 }, // Scully , Ivan , Dr Q , Thor , Len , Wolf , Blood
};
void MultiplayerTeams::SerializeProfiles(int* dest)
{
int id;
int r = Random(teams.size());
// if team is already used, choose next
while (teamsTaken.at(r))
{
r++;
if (r == teams.size())
r = 0;
}
teamsTaken.at(r) = true;
id = r;
this->SerializeProfiles(dest, id);
}
void MultiplayerTeams::SerializeProfiles(int* dest, int teamID)
{
teamID = teamID % this->teams.size();
for (int i = 0; i < RANDOM_MERC_TEAM_SIZE; i++)
{
// poor man's memcpy
*dest = teams.at(teamID).members.at(i);
dest++;
}
return;
}
void MultiplayerTeams::HandleServerStarted()
{
// reset
teamsTaken = std::vector<bool>(teams.size(), false);
}
void MultiplayerTeams::UseFallbackDataIfNecessary()
{
for (size_t team = 0; team < max(NUM_RANDOM_MERC_TEAMS, teams.size()); team++)
{
if (teams.size() <= team)
{
// create teams 1-4
teams.push_back(MPTeam());
}
// fill any empty merc slots
for (int merc = teams.at(team).members.size(); merc < RANDOM_MERC_TEAM_SIZE; merc++)
{
teams.at(team).members.push_back(MultiplayerTeams::random_merc_teams[team%NUM_RANDOM_MERC_TEAMS][merc]);
}
}
}
BOOLEAN MultiplayerTeams::ReadInMPTeams(STR fileName)
{
BOOLEAN result = this->ReadXMLFile(fileName);
this->UseFallbackDataIfNecessary();
return result;
}
BOOLEAN MultiplayerTeams::ReadXMLFile(STR fileName)
{
HWFILE hFile;
UINT32 uiBytesRead;
UINT32 uiFSize;
CHAR8 *lpcBuffer;
CHAR8 msg[128];
sprintf(msg, "Loading %s", fileName);
DebugMsg(TOPIC_JA2, DBG_LEVEL_3, msg);
if(!FileExists(fileName))
{
return FALSE;
}
// open file
hFile = FileOpen(fileName, FILE_ACCESS_READ, FALSE);
if (!hFile)
{
// this might issue an error in the caller
// but we can proceed, actually
return FALSE;
}
uiFSize = FileGetSize(hFile);
lpcBuffer = (CHAR*) MemAlloc(uiFSize);
// read in block
if (!FileRead(hFile, lpcBuffer, uiFSize, &uiBytesRead))
{
MemFree(lpcBuffer);
return FALSE;
}
FileClose(hFile);
XML_Parser parser = XML_ParserCreate(NULL);
struct teamsParseData parseData = { };
parseData.mpTeams = this;
XML_SetElementHandler(parser, &MultiplayerTeams::teamsStartElementHandler, &MultiplayerTeams::teamsEndElementHandler);
XML_SetCharacterDataHandler(parser, this->teamsCharacterDataHandler);
XML_SetUserData(parser, &parseData);
if (this->initialized)
this->TrashTeams();
if (!XML_Parse(parser, lpcBuffer, uiFSize, TRUE))
{
// Cannot load, but that's okay. There is fallback data.
MemFree(lpcBuffer);
XML_ParserFree(parser);
return FALSE;
}
else
this->initialized = true;
MemFree(lpcBuffer);
XML_ParserFree(parser);
return TRUE;
}
MultiplayerTeams::MultiplayerTeams()
{
this->initialized = false;
}
MultiplayerTeams::~MultiplayerTeams()
{
}
void MultiplayerTeams::TrashTeams()
{
this->teams.clear();
this->initialized = false;
}
void XMLCALL MultiplayerTeams::teamsStartElementHandler(void *userData, const XML_Char *name, const XML_Char **atts)
{
struct teamsParseData * pData = (struct teamsParseData *)userData;
if (pData->currentDepth <= pData->maxReadDepth)
{
if (!strcmp(name, "mp_teams") && pData->curElement == ELEMENT_NONE)
{
// root element
pData->curElement = ELEMENT_LIST;
pData->maxReadDepth++;
}
else if (!strcmp(name, "team") && pData->curElement == ELEMENT_LIST)
{
pData->curElement = ELEMENT;
MPTeam team = MPTeam();
if (atts[0] && !strcmp(atts[0], "name"))
{
team.name = std::string(atts[1]);
}
else
{
team.name = std::string("unnamed team");
}
pData->mpTeams->teams.push_back(team);
pData->maxReadDepth++;
}
else if (!strcmp(name, "id"))
{
pData->curElement = ELEMENT_PROPERTY;
pData->maxReadDepth++;
}
pData->szCharData[0] = '\0';
}
pData->currentDepth++;
return;
}
void XMLCALL MultiplayerTeams::teamsEndElementHandler(void *userData, const XML_Char *name)
{
struct teamsParseData *pData = (struct teamsParseData *)userData;
if (pData->currentDepth <= pData->maxReadDepth)
{
if (!strcmp(name, "mp_teams"))
{
pData->curElement = ELEMENT_NONE;
}
else if (!strcmp(name, "team"))
{
pData->curElement = ELEMENT_LIST;
}
else if(!strcmp(name, "id"))
{
pData->curElement = ELEMENT;
INT8 id = atoi(pData->szCharData);
pData->mpTeams->teams.back().members.push_back(id);
}
pData->maxReadDepth--;
}
pData->currentDepth--;
return;
}
void XMLCALL MultiplayerTeams::teamsCharacterDataHandler(void *userData, const XML_Char *s, int len)
{
struct teamsParseData *pData = (struct teamsParseData *)userData;
if ((pData->currentDepth <= pData->maxReadDepth)
&& (strlen(pData->szCharData) < MAX_CHAR_DATA_LENGTH))
{
strncat(pData->szCharData, s, __min((unsigned int)len, MAX_CHAR_DATA_LENGTH-strlen(pData->szCharData)));
}
return;
}