summaryrefslogtreecommitdiff
path: root/indra/newview/llagentpilot.cpp
blob: 4b3b0e42e02b0e15f2c1c69d222ebc5ae1ce0b47 (plain)
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
/** 
 * @file llagentpilot.cpp
 * @brief LLAgentPilot class implementation
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include <iostream>
#include <fstream>
#include <iomanip>

#include "llagentpilot.h"
#include "llagent.h"
#include "llappviewer.h"
#include "llviewercontrol.h"
#include "llviewercamera.h"
#include "llsdserialize.h"
#include "llsdutil_math.h"

LLAgentPilot gAgentPilot;

LLAgentPilot::LLAgentPilot() :
	mNumRuns(-1),
	mQuitAfterRuns(FALSE),
	mRecording(FALSE),
	mLastRecordTime(0.f),
	mStarted(FALSE),
	mPlaying(FALSE),
	mCurrentAction(0),
	mOverrideCamera(FALSE),
	mLoop(TRUE),
	mReplaySession(FALSE)
{
}

LLAgentPilot::~LLAgentPilot()
{
}

void LLAgentPilot::load()
{
	std::string txt_filename = gSavedSettings.getString("StatsPilotFile");
	std::string xml_filename = gSavedSettings.getString("StatsPilotXMLFile");
	if (LLFile::isfile(xml_filename))
	{
		loadXML(xml_filename);
	}
	else if (LLFile::isfile(txt_filename))
	{
		loadTxt(txt_filename);
	}
	else
	{
		LL_DEBUGS() << "no autopilot file found" << LL_ENDL;
		return;
	}
}

void LLAgentPilot::loadTxt(const std::string& filename)
{
	if(filename.empty())
	{
		return;
	}
	
	std::ifstream file(filename.c_str());

	if (!file)
	{
		LL_DEBUGS() << "Couldn't open " << filename
			<< ", aborting agentpilot load!" << LL_ENDL;
		return;
	}
	else
	{
		LL_INFOS() << "Opening pilot file " << filename << LL_ENDL;
	}

	mActions.clear();
	S32 num_actions;

	file >> num_actions;

	mActions.reserve(num_actions);
	for (S32 i = 0; i < num_actions; i++)
	{
		S32 action_type;
		Action new_action;
		file >> new_action.mTime >> action_type;
		file >> new_action.mTarget.mdV[VX] >> new_action.mTarget.mdV[VY] >> new_action.mTarget.mdV[VZ];
		new_action.mType = (EActionType)action_type;
		mActions.push_back(new_action);
	}

	mOverrideCamera = false;
	
	file.close();
}

void LLAgentPilot::loadXML(const std::string& filename)
{
	if(filename.empty())
	{
		return;
	}
	
	std::ifstream file(filename.c_str());

	if (!file)
	{
		LL_DEBUGS() << "Couldn't open " << filename
			<< ", aborting agentpilot load!" << LL_ENDL;
		return;
	}
	else
	{
		LL_INFOS() << "Opening pilot file " << filename << LL_ENDL;
	}

	mActions.clear();
	LLSD record;
	while (!file.eof() && LLSDParser::PARSE_FAILURE != LLSDSerialize::fromXML(record, file))
	{
		Action action;
		action.mTime = record["time"].asReal();
		action.mType = (EActionType)record["type"].asInteger();
		action.mCameraView = record["camera_view"].asReal();
		action.mTarget = ll_vector3d_from_sd(record["target"]);
		action.mCameraOrigin = ll_vector3_from_sd(record["camera_origin"]);
		action.mCameraXAxis = ll_vector3_from_sd(record["camera_xaxis"]);
		action.mCameraYAxis = ll_vector3_from_sd(record["camera_yaxis"]);
		action.mCameraZAxis = ll_vector3_from_sd(record["camera_zaxis"]);
		mActions.push_back(action);
	}
	mOverrideCamera = true;
	file.close();
}

void LLAgentPilot::save()
{
	std::string txt_filename = gSavedSettings.getString("StatsPilotFile");
	std::string xml_filename = gSavedSettings.getString("StatsPilotXMLFile");
	saveTxt(txt_filename);
	saveXML(xml_filename);
}

void LLAgentPilot::saveTxt(const std::string& filename)
{
	std::ofstream file;
	file.open(filename.c_str());

	if (!file)
	{
		LL_INFOS() << "Couldn't open " << filename << ", aborting agentpilot save!" << LL_ENDL;
	}

	file << mActions.size() << '\n';

	S32 i;
	for (i = 0; i < mActions.size(); i++)
	{
		file << mActions[i].mTime << "\t" << mActions[i].mType << "\t";
		file << std::setprecision(32) << mActions[i].mTarget.mdV[VX] << "\t" << mActions[i].mTarget.mdV[VY] << "\t" << mActions[i].mTarget.mdV[VZ];
		file << '\n';
	}

	file.close();
}

void LLAgentPilot::saveXML(const std::string& filename)
{
	std::ofstream file;
	file.open(filename.c_str());

	if (!file)
	{
		LL_INFOS() << "Couldn't open " << filename << ", aborting agentpilot save!" << LL_ENDL;
	}

	S32 i;
	for (i = 0; i < mActions.size(); i++)
	{
		Action& action = mActions[i];
		LLSD record;
		record["time"] = (LLSD::Real)action.mTime;
		record["type"] = (LLSD::Integer)action.mType;
		record["camera_view"] = (LLSD::Real)action.mCameraView;
		record["target"] = ll_sd_from_vector3d(action.mTarget);
		record["camera_origin"] = ll_sd_from_vector3(action.mCameraOrigin);
		record["camera_xaxis"] = ll_sd_from_vector3(action.mCameraXAxis);
		record["camera_yaxis"] = ll_sd_from_vector3(action.mCameraYAxis);
		record["camera_zaxis"] = ll_sd_from_vector3(action.mCameraZAxis);
		LLSDSerialize::toXML(record, file);
	}
	file.close();
}

void LLAgentPilot::startRecord()
{
	mActions.clear();
	mTimer.reset();
	addAction(STRAIGHT);
	mRecording = TRUE;
}

void LLAgentPilot::stopRecord()
{
	gAgentPilot.addAction(STRAIGHT);
	gAgentPilot.save();
	mRecording = FALSE;
}

void LLAgentPilot::addAction(enum EActionType action_type)
{
	LL_INFOS() << "Adding waypoint: " << gAgent.getPositionGlobal() << LL_ENDL;
	Action action;
	action.mType = action_type;
	action.mTarget = gAgent.getPositionGlobal();
	action.mTime = mTimer.getElapsedTimeF32();
	LLViewerCamera *cam = LLViewerCamera::getInstance();
	action.mCameraView = cam->getView();
	action.mCameraOrigin = cam->getOrigin();
	action.mCameraXAxis = cam->getXAxis();
	action.mCameraYAxis = cam->getYAxis();
	action.mCameraZAxis = cam->getZAxis();
	mLastRecordTime = (F32)action.mTime;
	mActions.push_back(action);
}

void LLAgentPilot::startPlayback()
{
	if (!mPlaying)
	{
		mPlaying = TRUE;
		mCurrentAction = 0;
		mTimer.reset();

		if (mActions.size())
		{
			LL_INFOS() << "Starting playback, moving to waypoint 0" << LL_ENDL;
			gAgent.startAutoPilotGlobal(mActions[0].mTarget);
			moveCamera();
			mStarted = FALSE;
		}
		else
		{
			LL_INFOS() << "No autopilot data, cancelling!" << LL_ENDL;
			mPlaying = FALSE;
		}
	}
}

void LLAgentPilot::stopPlayback()
{
	if (mPlaying)
	{
		mPlaying = FALSE;
		mCurrentAction = 0;
		mTimer.reset();
		gAgent.stopAutoPilot();
	}

	if (mReplaySession)
	{
		LLAppViewer::instance()->forceQuit();
	}
}

void LLAgentPilot::moveCamera()
{
	if (!getOverrideCamera())
		return;

	if (mCurrentAction<mActions.size())
	{
		S32 start_index = llmax(mCurrentAction-1,0);
		S32 end_index = mCurrentAction;
		F32 t = 0.0;
		F32 timedelta = mActions[end_index].mTime - mActions[start_index].mTime;
		F32 tickelapsed = mTimer.getElapsedTimeF32()-mActions[start_index].mTime;
		if (timedelta > 0.0)
		{
			t = tickelapsed/timedelta;
		}

		if ((t<0.0)||(t>1.0))
		{
			LL_WARNS() << "mCurrentAction is invalid, t = " << t << LL_ENDL;
			return;
		}
		
		Action& start = mActions[start_index];
		Action& end = mActions[end_index];

		F32 view = lerp(start.mCameraView, end.mCameraView, t);
		LLVector3 origin = lerp(start.mCameraOrigin, end.mCameraOrigin, t);
		LLQuaternion start_quat(start.mCameraXAxis, start.mCameraYAxis, start.mCameraZAxis);
		LLQuaternion end_quat(end.mCameraXAxis, end.mCameraYAxis, end.mCameraZAxis);
		LLQuaternion quat = nlerp(t, start_quat, end_quat);
		LLMatrix3 mat(quat);
	
		LLViewerCamera::getInstance()->setView(view);
		LLViewerCamera::getInstance()->setOrigin(origin);
		LLViewerCamera::getInstance()->mXAxis = LLVector3(mat.mMatrix[0]);
		LLViewerCamera::getInstance()->mYAxis = LLVector3(mat.mMatrix[1]);
		LLViewerCamera::getInstance()->mZAxis = LLVector3(mat.mMatrix[2]);
	}
}

void LLAgentPilot::updateTarget()
{
	if (mPlaying)
	{
		if (mCurrentAction < mActions.size())
		{
			if (0 == mCurrentAction)
			{
				if (gAgent.getAutoPilot())
				{
					// Wait until we get to the first location before starting.
					return;
				}
				else
				{
					if (!mStarted)
					{
						LL_INFOS() << "At start, beginning playback" << LL_ENDL;
						mTimer.reset();
						mStarted = TRUE;
					}
				}
			}
			if (mTimer.getElapsedTimeF32() > mActions[mCurrentAction].mTime)
			{
				//gAgent.stopAutoPilot();
				mCurrentAction++;

				if (mCurrentAction < mActions.size())
				{
					gAgent.startAutoPilotGlobal(mActions[mCurrentAction].mTarget);
					moveCamera();
				}
				else
				{
					stopPlayback();
					mNumRuns--;
					if (mLoop)
					{
						if ((mNumRuns < 0) || (mNumRuns > 0))
						{
							LL_INFOS() << "Looping, restarting playback" << LL_ENDL;
							startPlayback();
						}
						else if (mQuitAfterRuns)
						{
							LL_INFOS() << "Done with all runs, quitting viewer!" << LL_ENDL;
							LLAppViewer::instance()->forceQuit();
						}
						else
						{
							LL_INFOS() << "Done with all runs, disabling pilot" << LL_ENDL;
							stopPlayback();
						}
					}
				}
			}
		}
		else
		{
			stopPlayback();
		}
	}
	else if (mRecording)
	{
		if (mTimer.getElapsedTimeF32() - mLastRecordTime > 1.f)
		{
			addAction(STRAIGHT);
		}
	}
}

void LLAgentPilot::addWaypoint()
{
	addAction(STRAIGHT);
}