include/replay.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *            replay.h
00003  *
00004  *  Fri Jun 10 18:57:03 2005
00005  *  Copyright  2005  Joe Venzon
00006  *  joe@venzon.net
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 #ifndef _REPLAY_H
00026 
00027 #include <stdio.h>                      // Header File For Standard Input/Output
00028 #include <GL/gl.h>
00029 #include <GL/glu.h>
00030 #include <SDL/SDL.h>
00031 #ifdef __APPLE__
00032 #include <SDL_image/SDL_image.h>
00033 #else
00034 #include <SDL/SDL_image.h>
00035 #endif
00036 #include <string>
00037 #include <iostream>
00038 #include <fstream>
00039 #include <cmath>
00040 #include <cassert>
00041 
00042 using namespace std;
00043 
00044 #include <vamos/geometry/Three_Vector.h>
00045 #include <vamos/geometry/Three_Matrix.h>
00046 
00047 #include "utility.h"
00048 #include "timer.h"
00049 #include "gamestate.h"
00050 
00051 #include "globals.h"
00052 
00053 #define MAX_PACKETS 83000
00054 //#define MAX_PACKETS 100
00055 
00056 #define FUNCTION_CHARS 50
00057 
00058 #define MAX_STATES 3600
00059 
00060 #define REPLAY_VERSION "VDRIFTREPLAYV06"
00061 
00062 #define CHAR_FUNCNUM 0
00063 #define CHAR_STATE 1
00064 #define CHARDATA_SIZE 2
00065 
00066 #define FUNCTION_NETCONTROL 255
00067 #define FUNCTION_STATEINFO 254
00068 #define FUNCTION_NOOP 253
00069 #define FUNCTION_PACKET_ARRAY 252
00070 
00071 //how often we remember the car's state
00072 #define STATE_FREQUENCY 1.0
00073 
00074 #define DBG_REPLAY false
00075 
00076 struct REPLAY_PACKET
00077 {
00078         //char dofunction[FUNCTION_CHARS];
00079         //int funcnum;
00080         unsigned char chardata[CHARDATA_SIZE]; //funcnum is 0, state is 1
00081         float val;
00082         float time;
00083         //int state; //0 for one-time, 1 for held, 2 for stop holding
00084 };
00085 
00086 struct FUNCTION_MEMORY
00087 {
00088         string func_name;
00089         float oldval;
00090         float newval;
00091         bool held;
00092         bool active;
00093         double lastupdateat;
00094 };
00095 
00096 struct FUNCTION_MEMORY_SYNC
00097 {
00098         float oldval;
00099         float newval;
00100         bool held;
00101         bool active;
00102         double lastupdateat;
00103 };
00104 
00105 class CARSTATE
00106 {
00107 public:
00108         double time;
00109         
00110         Vamos_Geometry::Three_Vector chassispos;
00111         Vamos_Geometry::Three_Matrix chassisorientation;
00112         Vamos_Geometry::Three_Vector chassisvel;
00113         Vamos_Geometry::Three_Vector chassisangvel;
00114         double suspdisp[4];
00115         double suspcompvel[4];
00116         Vamos_Geometry::Three_Vector whlangvel[4];
00117         int gear;
00118         double enginespeed;
00119         double clutchspeed;
00120         double enginedrag;
00121         double tirespeed[4];
00122 
00123         int segment;
00124 
00125         FUNCTION_MEMORY_SYNC * funcmem;
00126         int fnum;
00127         
00128         void CopyFrom(CARSTATE other);
00129         
00130         void WriteToFile(FILE * fout);
00131         void WriteVector(Vamos_Geometry::Three_Vector wv, FILE * fout);
00132         void WriteMatrix(Vamos_Geometry::Three_Matrix wv, FILE * fout);
00133 
00134         void ReadFromFile(FILE * fin);
00135         void ReadVector(Vamos_Geometry::Three_Vector &wv, FILE * fin);
00136         void ReadMatrix(Vamos_Geometry::Three_Matrix &wv, FILE * fin);
00137 };
00138 
00139 class REPLAY
00140 {
00141 private:
00142         int num_packets;
00143         REPLAY_PACKET packet[MAX_PACKETS];
00144         FUNCTION_MEMORY * funcmem;
00145         int fnum;
00146         bool replay;
00147         FILE * rf;
00148         double timeindex;
00149         bool playing;
00150         int cur_packet;
00151         void PlayProcessFrame();
00152         float playend;
00153 
00154         void WriteCarState();
00155         void RememberCarState();
00156         CARSTATE state_mem[MAX_STATES];
00157         int num_states;
00158         CARSTATE loadstate;
00159         bool state_to_load;
00160         int cur_state;
00161 
00162         CARSTATE * GetNextState(double until);
00163         REPLAY_PACKET * GetNextPacket(double until);
00164         
00165         bool ghostcar;
00166         
00167         string replaycar;
00168         int replaypaint;
00169 
00170 public:
00171         void BuildFuncMem(FUNCTION_MEMORY * &fmem, int & fnm);
00172 
00173         REPLAY();
00174         ~REPLAY();
00175         void Clear();
00176         void Start();
00177         void AddRecord(string newdofunction, float newval, int newstate);
00178         void IncrementFrame();
00179         void Stop();
00180         float Recording();
00181 
00182         bool PlayStart(string replayname);
00183         bool PlayStart(string replayname, bool ngc);
00184         void PlayStop();
00185         float Playing();
00186         int GetNumFuncs();
00187         FUNCTION_MEMORY GetFunc(int idx);
00188         int GetFuncIdx(string funcname);
00189         void PlayIncrement();
00190         double GetTime();
00191 
00192         CARSTATE curstate;
00193         CARSTATE * LoadState();
00194         //CARSTATE GetState(int idx) {return state_mem[idx];}
00195         
00196         void Jump(double newtime);
00197         
00198         //extract constants
00199         int Get_FuncChars() {return FUNCTION_CHARS;}
00200         int Get_CharState() {return CHAR_STATE;}
00201         int Get_CharFuncnum() {return CHAR_FUNCNUM;}
00202         int Get_FuncNetControl() {return FUNCTION_NETCONTROL;}
00203         int Get_FuncStateInfo() {return FUNCTION_STATEINFO;}
00204         int Get_FuncNoop() {return FUNCTION_NOOP;}
00205         int Get_FuncPacketArray() {return FUNCTION_PACKET_ARRAY;}
00206         
00207         //file writing routines
00208         void WriteHeader(FILE * rf, FUNCTION_MEMORY * fmem, int fnm, int carnumber);
00209         void WritePackets(FILE * rf, REPLAY_PACKET * pak, int pnum, CARSTATE * smem, int snum);
00210         
00211         bool GhostCar() {return ghostcar;}
00212         string ReplayCar() {return replaycar;}
00213         int ReplayPaint() {return replaypaint;}
00214 };
00215 
00216 #define _REPLAY_H
00217 #endif /* _REPLAY_H */

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6