llagentpilot.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include <iostream>
00035 #include <fstream>
00036 #include <iomanip>
00037 
00038 #include "llagentpilot.h"
00039 #include "llagent.h"
00040 #include "llframestats.h"
00041 #include "viewer.h"
00042 #include "llviewercontrol.h"
00043 
00044 LLAgentPilot gAgentPilot;
00045 
00046 BOOL LLAgentPilot::sLoop = TRUE;
00047 
00048 LLAgentPilot::LLAgentPilot()
00049 {
00050         mRecording = FALSE;
00051         mPlaying = FALSE;
00052         mStarted = FALSE;
00053         mNumRuns = -1;
00054 }
00055 
00056 LLAgentPilot::~LLAgentPilot()
00057 {
00058 }
00059 
00060 void LLAgentPilot::load(const char *filename)
00061 {
00062         if(!filename) return;
00063 
00064         llifstream file(filename);
00065 
00066         if (!file)
00067         {
00068                 lldebugs << "Couldn't open " << filename
00069                         << ", aborting agentpilot load!" << llendl;
00070                 return;
00071         }
00072         else
00073         {
00074                 llinfos << "Opening pilot file " << filename << llendl;
00075         }
00076 
00077         S32 num_actions;
00078 
00079         file >> num_actions;
00080 
00081         S32 i;
00082         for (i = 0; i < num_actions; i++)
00083         {
00084                 S32 action_type;
00085                 Action new_action;
00086                 file >> new_action.mTime >> action_type;
00087                 file >> new_action.mTarget.mdV[VX] >> new_action.mTarget.mdV[VY] >> new_action.mTarget.mdV[VZ];
00088                 new_action.mType = (EActionType)action_type;
00089                 mActions.put(new_action);
00090         }
00091 
00092         file.close();
00093 }
00094 
00095 void LLAgentPilot::save(const char *filename)
00096 {
00097         llofstream file;
00098         file.open(filename);                    /*Flawfinder: ignore*/
00099 
00100         if (!file)
00101         {
00102                 llinfos << "Couldn't open " << filename << ", aborting agentpilot save!" << llendl;
00103         }
00104 
00105         file << mActions.count() << '\n';
00106 
00107         S32 i;
00108         for (i = 0; i < mActions.count(); i++)
00109         {
00110                 file << mActions[i].mTime << "\t" << mActions[i].mType << "\t";
00111                 file << std::setprecision(32) << mActions[i].mTarget.mdV[VX] << "\t" << mActions[i].mTarget.mdV[VY] << "\t" << mActions[i].mTarget.mdV[VZ] << '\n';
00112         }
00113 
00114         file.close();
00115 }
00116 
00117 void LLAgentPilot::startRecord()
00118 {
00119         mActions.reset();
00120         mTimer.reset();
00121         addAction(STRAIGHT);
00122         mRecording = TRUE;
00123 }
00124 
00125 void LLAgentPilot::stopRecord()
00126 {
00127         gAgentPilot.addAction(STRAIGHT);
00128         gAgentPilot.save(gSavedSettings.getString("StatsPilotFile").c_str());
00129         mRecording = FALSE;
00130 }
00131 
00132 void LLAgentPilot::addAction(enum EActionType action_type)
00133 {
00134         llinfos << "Adding waypoint: " << gAgent.getPositionGlobal() << llendl;
00135         Action action;
00136         action.mType = action_type;
00137         action.mTarget = gAgent.getPositionGlobal();
00138         action.mTime = mTimer.getElapsedTimeF32();
00139         mLastRecordTime = (F32)action.mTime;
00140         mActions.put(action);
00141 }
00142 
00143 void LLAgentPilot::startPlayback()
00144 {
00145         if (!mPlaying)
00146         {
00147                 mPlaying = TRUE;
00148                 mCurrentAction = 0;
00149                 mTimer.reset();
00150 
00151                 if (mActions.count())
00152                 {
00153                         llinfos << "Starting playback, moving to waypoint 0" << llendl;
00154                         gAgent.startAutoPilotGlobal(mActions[0].mTarget);
00155                         mStarted = FALSE;
00156                 }
00157                 else
00158                 {
00159                         llinfos << "No autopilot data, cancelling!" << llendl;
00160                         mPlaying = FALSE;
00161                 }
00162         }
00163 }
00164 
00165 void LLAgentPilot::stopPlayback()
00166 {
00167         if (mPlaying)
00168         {
00169                 mPlaying = FALSE;
00170                 mCurrentAction = 0;
00171                 mTimer.reset();
00172                 gAgent.stopAutoPilot();
00173         }
00174 }
00175 
00176 void LLAgentPilot::updateTarget()
00177 {
00178         if (mPlaying)
00179         {
00180                 if (mCurrentAction < mActions.count())
00181                 {
00182                         if (0 == mCurrentAction)
00183                         {
00184                                 if (gAgent.getAutoPilot())
00185                                 {
00186                                         // Wait until we get to the first location before starting.
00187                                         return;
00188                                 }
00189                                 else
00190                                 {
00191                                         if (!mStarted)
00192                                         {
00193                                                 llinfos << "At start, beginning playback" << llendl;
00194                                                 mTimer.reset();
00195                                                 LLFrameStats::startLogging(NULL);
00196                                                 mStarted = TRUE;
00197                                         }
00198                                 }
00199                         }
00200                         if (mTimer.getElapsedTimeF32() > mActions[mCurrentAction].mTime)
00201                         {
00202                                 //gAgent.stopAutoPilot();
00203                                 mCurrentAction++;
00204 
00205                                 if (mCurrentAction < mActions.count())
00206                                 {
00207                                         gAgent.startAutoPilotGlobal(mActions[mCurrentAction].mTarget);
00208                                 }
00209                                 else
00210                                 {
00211                                         stopPlayback();
00212                                         LLFrameStats::stopLogging(NULL);
00213                                         mNumRuns--;
00214                                         if (sLoop)
00215                                         {
00216                                                 if ((mNumRuns < 0) || (mNumRuns > 0))
00217                                                 {
00218                                                         llinfos << "Looping, restarting playback" << llendl;
00219                                                         startPlayback();
00220                                                 }
00221                                                 else if (mQuitAfterRuns)
00222                                                 {
00223                                                         llinfos << "Done with all runs, quitting viewer!" << llendl;
00224                                                         app_force_quit(NULL);
00225                                                 }
00226                                                 else
00227                                                 {
00228                                                         llinfos << "Done with all runs, disabling pilot" << llendl;
00229                                                         stopPlayback();
00230                                                 }
00231                                         }
00232                                 }
00233                         }
00234                 }
00235                 else
00236                 {
00237                         stopPlayback();
00238                 }
00239         }
00240         else if (mRecording)
00241         {
00242                 if (mTimer.getElapsedTimeF32() - mLastRecordTime > 1.f)
00243                 {
00244                         addAction(STRAIGHT);
00245                 }
00246         }
00247 }
00248 
00249 // static
00250 void LLAgentPilot::startRecord(void *)
00251 {
00252         gAgentPilot.startRecord();
00253 }
00254 
00255 void LLAgentPilot::saveRecord(void *)
00256 {
00257         gAgentPilot.stopRecord();
00258 }
00259 
00260 void LLAgentPilot::addWaypoint(void *)
00261 {
00262         gAgentPilot.addAction(STRAIGHT);
00263 }
00264 
00265 void LLAgentPilot::startPlayback(void *)
00266 {
00267         gAgentPilot.mNumRuns = -1;
00268         gAgentPilot.startPlayback();
00269 }
00270 
00271 void LLAgentPilot::stopPlayback(void *)
00272 {
00273         gAgentPilot.stopPlayback();
00274 }

Generated on Thu Jul 1 06:08:18 2010 for Second Life Viewer by  doxygen 1.4.7