-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReplayer.cpp
225 lines (189 loc) · 5.2 KB
/
Replayer.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
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Replayer.h"
#include "Recorder.h"
#include "globals.h"
Replayer::Replayer() : active(false), first(true), starttick(0), filetick(0)
{}
void Replayer::SetActive(const string& filename, const bool a)
{
if (a && !active) // Read header information
{
logout << "Loading replay " << filename << endl;
player[0].spectate = true;
player[0].spawned = true;
items.clear();
int version, minor;
string fullpath = userpath + Recorder::savepath + filename;
read.close();
read.open(fullpath.c_str());
if (!read)
{
logout << "Failed to open replay file " << fullpath << endl;
return;
}
read >> version >> minor;
if (version != Recorder::version) // minor is for potential future use to differentiate different, but compatible file versions
{
logout << "Replay file version mismatch\n";
read.close();
return;
}
string nextmap;
read >> nextmap;
read >> filetick;
replaying = true;
first = true;
servplayernum = 0;
LoadMap(nextmap);
#ifndef DEDICATED
gui[loadoutmenu]->visible = false;
#endif
}
else if (!a)
{
read.close();
replaying = false;
}
active = a;
}
void Replayer::Update()
{
if (!active || !read)
return;
player[0].spawned = true; // This gets reset when the map loads
if (!spectateplayer && player.size() > 1)
spectateplayer = 1;
// The map will load after we call SetActive, and we don't want to include that time in our replay
// calculations, so we don't start the timers until the first time through update
if (first)
{
starttick = SDL_GetTicks();
timer.start();
first = false;
}
Uint32 currtick = SDL_GetTicks();
Uint32 virtualtick = currtick - starttick;
while (virtualtick > filetick)
{
ReadPlayers();
ReadShots();
ReadHits();
ReadItems();
if (!(read >> filetick))
{
// Output performance stats
logout << "Time: " << (timer.elapsed() / 1000.f) << " seconds" << endl;;
logout << "Frames: " << framecount << endl;
logout << "Avg. FPS: " << ((float)framecount / (float)timer.elapsed() * 1000.f) << endl;
break;
}
}
++framecount;
}
void Replayer::ReadPlayers()
{
size_t numplayers;
read >> numplayers;
for (size_t i = 1; i < player.size(); ++i)
player[i].spawned = false;
for (size_t i = 0; i < numplayers; ++i)
{
size_t pnum;
read >> pnum;
EnsurePlayerSize(pnum);
// Position
read >> player[pnum].pos.x >> player[pnum].pos.y >> player[pnum].pos.z;
// Rotations
read >> player[pnum].rotation >> player[pnum].facing >> player[pnum].pitch;
read >> player[pnum].speed;
player[pnum].spawned = true; // Only players updated in the current frame are spawned
}
// Occasional updates
size_t occ;
read >> occ;
if (occ)
{
for (size_t i = 1; i < occ; ++i)
{
EnsurePlayerSize(i);
read >> player[i].team;
read >> player[i].unit;
read >> player[i].kills;
read >> player[i].deaths;
read.ignore(); // Skip \n
getline(read, player[i].name);
}
}
}
void Replayer::ReadShots()
{
size_t numshots;
read >> numshots;
for (size_t i = 0; i < numshots; ++i)
{
size_t pnum;
int weapid;
read >> pnum >> weapid;
ClientCreateShot(player[pnum], weapid);
}
}
void Replayer::ReadHits()
{
size_t numhits;
read >> numhits;
for (size_t i = 0; i < numhits; ++i)
{
Vector3 pos;
int type;
read >> pos.x >> pos.y >> pos.z;
read >> type;
AddHit(pos, type);
}
}
void Replayer::ReadItems()
{
size_t numitems;
read >> numitems;
for (size_t i = 0; i < numitems; ++i)
{
Vector3 pos;
unsigned long id;
int type, team;
read >> pos.x >> pos.y >> pos.z;
read >> type >> team >> id;
Item newitem(type, meshes);
newitem.position = pos;
newitem.team = team;
newitem.id = id;
#ifndef DEDICATED
netcode->AddItem(newitem);
#endif
}
}
// Make sure that there are at least s players in the player vector
void Replayer::EnsurePlayerSize(const size_t s)
{
while (s >= player.size()) // Make sure we have the requisite number of players
{
PlayerData dummy(meshes);
player.push_back(dummy);
logout << "Replay adding player " << (player.size() - 1) << endl;
}
}